6

I'm interested in continuations, specifically in Python's C-API. From what i understand, the nature of continuations requires un-abstracting low-level calling conventions in order to manipulate the call stack as needed. I was fortunate enough to come across a few examples of these scattered here and there. In the few examples i've come across, this un-abstraction is done using either clever C (with assumptions about the environment), or custom assembly.

However, what's cool about Python is that it has its own interpreter stack made up of PyFrameObjects. Assuming single-threaded applications for now, shouldn't it be enough to just switch in-and-out PyFrameObjectss to implement continuations in Python's C-API? Why do these authors even bother with the low-level stuff?

Peter Varo
  • 11,726
  • 7
  • 55
  • 77
Noob Saibot
  • 4,573
  • 10
  • 36
  • 60
  • I can't really answer this, but optimization comes to mind. Good question! – ThinkChaos Nov 07 '14 at 18:12
  • 2
    Swapping frame objects is basically how generator functions work. When you hit `yield`, the current frame object is saved on the generator, and popped from the stack. When the next value is needed, the frame object is pushed back onto the stack, and execution continues where it left off. – Ned Batchelder Nov 07 '14 at 19:44

1 Answers1

1

Generators work by manipulating the stack (actually linked list) of frame objects. But that will only help for pure Python code. It won't help you if your code has any C code running. For example, if you are in C code inside an I/O routine, you can't change the Python frame object to get execution to go somewhere else. You have to be able to change the C stack in order to do that. That's what packages like greenlets do for you.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • I think this is the answer. Curiously, I think that if you don't need the parts implemented in C to participate in the continuation fest, manipulation of `PyFrameObject`s *would* be enough. In fact this is more or less proven by the generators and coroutines. – Julian Nov 13 '14 at 19:24