473,569 Members | 2,791 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1472
ro*********@gma il.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...@gma il.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
4338
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 maybe nothing is available yet. Doug
28
4828
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 python.org, then downloaded the binary python2.3 release of stackless python from www.stackless.com. This contained three files: python23.dll,...
0
1656
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 a project written in Python." - Paul Graham http://www.paulgraham.com/gh.html Colin J. Williams shows Mizrandir how to subclass numarray's...
5
3227
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. One of my favorite Python features is generators. I have to use Java for one particular project (it happens). I would like to have something at
7
1190
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. The latter seems to focus on how to do it, rather than why you'd do it, so it doesn't aid the understanding too much. -- Ben Sizer
852
28142
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 my general education. Mark
0
1179
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´ 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...
12
1248
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: Don't call me millionaire; life & money...). It is not clear that I will be posting code first thing, for a range of values of I. (Yes I have...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.