Freeze and Resume execution

Shalabh Chaturvedi shalabh at cafepy.com
Thu May 20 20:26:12 EDT 2004


Miki Tebeka wrote:

> Hello All,
> 
> I'm looking for a way to "yield" an exception.
> 
> Background: I'm writing a hardware simulator. I have an output buffer
> and need to freeze when it's full and then when called again to resume
> execution from the point where it stopped.
> 
> Currently all I can think of is to use a class and save the state when
> throwing an exception. However this required manual book keeping of the
> current state (and it is complicated).
> 
> I'd like to use generators but can't see any "nice" way of doing it.
> What I'd like it to throw an exception when the buffer is full and then
> next time the generator is called to continue execution as after a
> "yield".
> 
> Is this possible?
> Can you recommend a good way of doing this? Any state machine?

Python generators yield to only one level up in the stack frame - i.e. the
calling function. Exceptions, however, are thrown up the stack any number
of levels, but there is no way to resume after that.

What you could try is to yield and then call the generator again. If you
want to pass information back and forth you can yield a mutable (like []),
put values into it from the caller, and then call the generator again.

If you want to yield multiple levels up the stack frame and later resume
where you left off, you *might* want to look at stackless. It may or may
not do what you want.

> 
> Thanks.
> Bye.

--
Shalabh






More information about the Python-list mailing list