Steven H. Rogers | 7 Mar 2007 03:46
Favicon

Re: [Simpy-developer] Fwd: Pickling of generators

Gregory Smith wrote:
> Not the entire C stack. generators can call arbitrary functions
> (including C extensions), but when they have reached a yield statement,
> none of that is
> active and Python only has the one stack frame for the coroutine. This
> is what allows generators in the first place - yield is treated as a
> 'return' statement (to the caller of .next()) but the frame of the
> active generator is preserved instead of being destroyed as in a normal
> return. The frame includes the local variables, current execution
> position, and block stack (while,for, try blocks etc).
> 
> Multiple threads have multiple C stacks - but generators run on the same
> C stack as whichever thread has called 'next()'.
> 
> Look at it this way - you can call 15 levels of python (building up a
> big
> C stack in the process), create a generator, call next() once, then
> return
> that generator through 15 levels of python, and it still works. Clearly
> it's
> not tied to the 'under' stack, since that can be reduced arbitrarily and
> it
> still works - and it's only tied to the 'over' stack when it's actually
> being executed (while next() is being called). There is no 'over' stack
> when next() returns. 
> 
> There is a flag in the generator object which indicates whether the gen.
> is running; this is set when next() is active, and is presumably to
> detect a case where the generator somehow calls its own 'next' method,
> or if another thread tries to call next(), both of which would be
> disastrous since you can't share a frame object like that. It would only
> be possible to pickle a generator when the flag is clear.
> 
> 
OK.  So it may be easier than I thought.

# Steve

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

Gmane