Freeze and Resume execution

Peter Hansen peter at engcorp.com
Sun May 23 09:12:37 EDT 2004


Miki Tebeka wrote:

>>>I'd like to use generators but can't see any "nice" way of doing it.
>>
>>That would be the approach I would take.  What can't you see
>>with using yield this way?
> 
> The output function is way down the call stack. The function that handles
> the "freezing" and need to be saved is about 3-5 stack frames up
> (depends on the scenario).
> 
> Propagating the output return value all the way back is tedious and
> error prone. This is why I'd like to use raise/catch and not "if error
> ..."

I start to understand, a bit.  Sounds like the suggestions to
use Stackless (and tasklets) are on the right track.

Alternatively, could you just use threads and have the output function
block (and force a context switch) when the buffer is full?

If you can't use threads, and you can't use Stackless, and if
exceptions don't allow restarting, and if generators preserve
only the current frame and not the entire stack (i.e. you can't
hand the generator state up to a higher level function, then
resume from there, which would be more like a coroutine I guess),
then it looks like handling this with classes/state machines is
your only option.

-Peter



More information about the Python-list mailing list