473,387 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Python generators (coroutines)

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.
Jun 27 '08 #1
3 1463
ro*********@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
Jun 27 '08 #2
On Apr 23, 4:17 pm, rocco.ro...@gmail.com 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,
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
Jun 27 '08 #3
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.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Doug | last post by:
I am thinking of using a Python based HTTP server instead of Apache. I would be interested in one that employed generators and coroutines. I know those are fairly new features of python, so...
28
by: Matt Leslie | last post by:
Hi, I'm trying to use microthreads under stackless python, since they sound like exactly what I am after, but I am having very little success. I've got a fresh install of python 2.3.3 from...
0
by: Peter Otten | last post by:
QOTW "Implementations are always preferred over rhetoric." - Robert Brewer "The programmers you'll be able to hire to work on a Java project won't be as smart as the ones you could get to work on...
5
by: Robert Oschler | last post by:
Preamble: - I know this is the Python forum - I know about (and have used) Jython I already posted this question in comp.lang.java. But after a week I have still not received a single reply....
7
by: Ben Sizer | last post by:
A simple question - can anybody give a short example of how these work and what they are good for? I've read PEP 342 and the associated bit in the What's New section and it's still all Greek to me....
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
0
by: Jean-Paul Calderone | last post by:
On Wed, 23 Apr 2008 07:17:46 -0700 (PDT), rocco.rossi@gmail.com wrote: They're not coroutines. The difference between generators in Python 2.4 and in Python 2.5 is that in Python 2.5, `yield´...
12
by: castironpi | last post by:
I want to talk to the newsgroup. As I have found that its readers will be non-trivially attentive, I esteem it a worthwhile production, for finely divided values of worth & while. (Disclaimer:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.