Python generators (coroutines)
Diez B. Roggisch
deets at nospam.web.de
Wed Apr 23 10:34:11 EDT 2008
rocco.rossi at gmail.com schrieb:
> 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,
> unfortunately.
What do you mean by "new generator characteristics"? AFAIK generators
have been around at least since python2.3. Newer versions of python
include things like generator expressions (since 2.4 I believe).
However, generators don't help anything in blocking IO-situations,
because they rely on co-operatively re-scheduling using yield.
Of course you could try & use non-blocking IO with them, but I don't see
that there is much to them.
Or you could go & use a single poll/select in your main-thread, and on
arrival of data process that data with generators that re-schedule in
between so that you can react on newer data. Depending on your scenario
this might reduce latency.
Diez
More information about the Python-list
mailing list