Generators versus Coroutines

Michael Sparks zathras at thwackety.com
Sat Aug 14 17:49:22 EDT 2004


On 14 Aug 2004, Timothy Fitz wrote:

> It seems to me that in python, generators are not truly coroutines.

Assuming you mean there isn't available a default scheduler for them, or
there isn't pre-emption built-in I agree. If you mean something else, I'm
curious as to what you think is missing. (I've also been using generators
as co-routines for sometime for various reasons)

> Why can I use "return" without an expression and it implicitly
> returns None but I can't do the same thing with "yield" ?

I suspect it's one of two things:
   * A syntax for yield by itself as either not considered important or
     needed.
   * The ability to do a return with an implicit None value was considered
     a mistake, but one that can't be undone this late in the game.

After all, the following works fine:
   * yield None

Also given I think the most expected use case for generators is to lazily
return a list, or to transform (or create) an iterator (eg a lexer),
having simply "yield" by itself might've been considered sensible.

That said, enforcing a return value is useful - even in the co-routine
situation since I find it useful for sending back information to
the scheduler.

BTW, if you're looking at using generators as co-routines, you might want
to look at Armin Rigo's "Greenlets" which whilst in the stackless python
tree work fine with standard python. (check out the CVS tree, and just
grab the greenlets directory - it built cleanly for me) As far as I can
tell Armin is *really* good at producing things that confound, astound
and speed up your code with minimal effort...

I've found that by replacing usage of generators-as-co-routines with
Greenlets that it speeds things up by a factor of about 2.5.  YMMV.
Incidentally the default mechanism for switching between greenlets is
a switch() method which can take 0 arguments.


Michael.




More information about the Python-list mailing list