Python generators (coroutines)

Jean-Paul Calderone exarkun at
Wed Apr 23 16:53:06 CEST 2008

On Wed, 23 Apr 2008 07:17:46 -0700 (PDT), rocco.rossi at wrote:
>I would really like to know more about python 2.5's new generator
>characteristics that make them more powerful and analogous to
>coroutines. Is it possible for instance to employ them in situations
>where I would normally use a thread with a blocking I/O (or socket)
>operation? If it is, could someone show me how it can be done? There
>appears to be a very limited amount of documentation in this repect,

They're not coroutines.  The difference between generators in Python 2.4
and in Python 2.5 is that in Python 2.5, `yield´ can be used as part of
an expression.  If a generator is resumed via its `send´ method, then the
`yield´ expression evaluates to the value passed to `send´.

You still can't suspend execution through arbitrary stack frames; `yield´
only suspends the generator it is in, returning control to the frame
immediately above it on the stack.

You can build a trampoline based on generators, but you can do that in
Python 2.4 just as easily as you can do it in Python 2.5.


More information about the Python-list mailing list