Python generators (coroutines)

R

rocco.rossi

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.

Thank you.
 
D

Diez B. Roggisch

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
 
M

Michele Simionato

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.

Thank you.

The real changes between Python 2.4 and Python 2.5 generators are
1) now you can have a yield inside a try .. finally statement
2) now you can send an exception to a generator

The fact that now you can send values to a generator
is less important, since you could implement the
same in Python 2.4 with little effort (granted, with an uglier syntax)
whereas there was no way to get 1) and 2).
Anyway, if you have a blocking operation, the only solution is to use
a thread or a separate process.

Michele Simionato
 
R

rocco.rossi

Anyway, if you have a blocking operation, the only solution is to use
a thread or a separate process.

Michele Simionato

That's what I thought. It was in fact rather obvious, but I wanted to
be sure that I hadn't overlooked some arcane possibility (ex. with the
use of exceptions or something like that). Thanks for the confirmation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top