473,773 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

n00b question: Better Syntax for Coroutines?

ig
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know if there's
any way to hide a yield statement.

I have a class that I'd like to look like this:

class Pinger(Actor):
def go(self):
success = True
while success:
result = self.ping("128. 111.41.38")
if result != "success":
success = False
print "Pinger done"

But because I can't hide yield inside ping, and because I can't find a
convenient way to get a self reference to the coroutine (which is used
by the event queue to pass back results), my code looks like this:

class Pinger(Actor):
def go(self):
# I dislike this next line
self.this_point er = (yield None)
success = True
while success:
# I want to get rid of the yield in the next line
result = (yield self.ping("128. 111.41.38"))
if result != "success":
success = False
print "Pinger done"

I posted a more detailed version of this as a rant here:
http://illusorygoals.com/post/49926627/

I'd like to know, is there a way to get the syntax I want? After
staying up late last night to get a proof-of-concept working with
coroutines, my boss expressed disappointment in the ugliness of the
Pinger code (we agreed on the desired syntax above). I spent almost 2
hours today to migrate the system over to threads. That made my boss
happy, but I'm still curious if I can do something to salvage the
coroutine version.

Regards,
IG
Sep 13 '08 #1
4 1268
On Sep 12, 8:08*pm, i...@illusorygo als.com wrote:
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know if there's
any way to hide a yield statement.

I have a class that I'd like to look like this:

class Pinger(Actor):
* * def go(self):
* * * * success = True
* * * * while success:
* * * * * * result = self.ping("128. 111.41.38")
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

But because I can't hide yield inside ping, and because I can't find a
convenient way to get a self reference to the coroutine (which is used
by the event queue to pass back results), my code looks like this:

class Pinger(Actor):
* * def go(self):
* * * * # I dislike this next line
* * * * self.this_point er = (yield None)
* * * * success = True
* * * * while success:
* * * * * * # I want to get rid of the yield in the next line
* * * * * * result = (yield self.ping("128. 111.41.38"))
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

I posted a more detailed version of this as a rant here:http://illusorygoals.com/post/49926627/

I'd like to know, is there a way to get the syntax I want? After
staying up late last night to get a proof-of-concept working with
coroutines, my boss expressed disappointment in the ugliness of the
Pinger code (we agreed on the desired syntax above). I spent almost 2
hours today to migrate the system over to threads. That made my boss
happy, but I'm still curious if I can do something to salvage the
coroutine version.

Regards,
IG
I did not read you whole post though I did skim your link. I don't
know your whole problem but it seems you are trying to inform a
generator of its own identity. Here's a solution to that problem;
perhaps it relates to yours. (The shorter retort is, "Just use
closures.")

def gen( ):
def _gen( ):
while 1:
yield 1, 'i am %r'% ob
yield 2, 'i am %r'% ob
yield 3, 'i am %r'% ob
ob= _gen( )
return ob

gens= [ gen( ) for _ in range( 4 ) ]
for i in range( 6 ):
print i
for g in gens:
print g.next( )

/Output (lengthy):

0
(1, 'i am <generator object at 0x009FEC10>')
(1, 'i am <generator object at 0x009FEC38>')
(1, 'i am <generator object at 0x009FEC60>')
(1, 'i am <generator object at 0x009FEC88>')
1
(2, 'i am <generator object at 0x009FEC10>')
(2, 'i am <generator object at 0x009FEC38>')
(2, 'i am <generator object at 0x009FEC60>')
(2, 'i am <generator object at 0x009FEC88>')
2
(3, 'i am <generator object at 0x009FEC10>')
(3, 'i am <generator object at 0x009FEC38>')
(3, 'i am <generator object at 0x009FEC60>')
(3, 'i am <generator object at 0x009FEC88>')
3
(1, 'i am <generator object at 0x009FEC10>')
(1, 'i am <generator object at 0x009FEC38>')
(1, 'i am <generator object at 0x009FEC60>')
(1, 'i am <generator object at 0x009FEC88>')
4
(2, 'i am <generator object at 0x009FEC10>')
(2, 'i am <generator object at 0x009FEC38>')
(2, 'i am <generator object at 0x009FEC60>')
(2, 'i am <generator object at 0x009FEC88>')
5
(3, 'i am <generator object at 0x009FEC10>')
(3, 'i am <generator object at 0x009FEC38>')
(3, 'i am <generator object at 0x009FEC60>')
(3, 'i am <generator object at 0x009FEC88>')
Sep 13 '08 #2
On Sep 12, 9:08*pm, i...@illusorygo als.com wrote:
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know if there's
any way to hide a yield statement.

I have a class that I'd like to look like this:

class Pinger(Actor):
* * def go(self):
* * * * success = True
* * * * while success:
* * * * * * result = self.ping("128. 111.41.38")
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

But because I can't hide yield inside ping, and because I can't find a
convenient way to get a self reference to the coroutine (which is used
by the event queue to pass back results), my code looks like this:

class Pinger(Actor):
* * def go(self):
* * * * # I dislike this next line
* * * * self.this_point er = (yield None)
* * * * success = True
* * * * while success:
* * * * * * # I want to get rid of the yield in the next line
* * * * * * result = (yield self.ping("128. 111.41.38"))
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

I'd like to know, is there a way to get the syntax I want?
I think you're stuck with using threads in a standard Python release.
Generators can't serve as coroutines when you're yielding from a
nested call (they only go one level deep).

You can get coroutines with Stackless Python, a non-standard version
of Python. But even with Stackless I got the impression that you'd be
building coroutines atop something that was fairly thread-like. There
is no coroutine syntax.
Carl Banks
Sep 13 '08 #3
On Sep 13, 2:08*am, i...@illusorygo als.com wrote:
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know if there's
any way to hide a yield statement.

I have a class that I'd like to look like this:

class Pinger(Actor):
* * def go(self):
* * * * success = True
* * * * while success:
* * * * * * result = self.ping("128. 111.41.38")
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

But because I can't hide yield inside ping, and because I can't find a
convenient way to get a self reference to the coroutine (which is used
by the event queue to pass back results), my code looks like this:

class Pinger(Actor):
* * def go(self):
* * * * # I dislike this next line
* * * * self.this_point er = (yield None)
* * * * success = True
* * * * while success:
* * * * * * # I want to get rid of the yield in the next line
* * * * * * result = (yield self.ping("128. 111.41.38"))
* * * * * * if result != "success":
* * * * * * * * success = False
* * * * print "Pinger done"

I posted a more detailed version of this as a rant here:http://illusorygoals.com/post/49926627/

I'd like to know, is there a way to get the syntax I want? After
staying up late last night to get a proof-of-concept working with
coroutines, my boss expressed disappointment in the ugliness of the
Pinger code (we agreed on the desired syntax above). I spent almost 2
hours today to migrate the system over to threads. That made my boss
happy, but I'm still curious if I can do something to salvage the
coroutine version.

Regards,
IG
You can't avoid the yield, and generators are not coroutines. A
little while ago when thinking about this kind of problem I defined
"cogenerato rs", which roughly are to generators what coroutines are to
routines, i.e. they can pass "yielding control" on to another
cogenerator [1].

[1] http://www.marooned.org.uk/~arno/pyt...generator.html

--
Arnaud

Sep 13 '08 #4
On Sep 12, 8:08*pm, i...@illusorygo als.com wrote:
First off, I'm a python n00b, so feel free to comment on anything if
I'm doing it "the wrong way." I'm building a discrete event simulation
tool. I wanted to use coroutines. However, I want to know if there's
You could "look in the back of the book" - download SimPy and see how
they does this.

-- Paul
Sep 13 '08 #5

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

Similar topics

8
2260
by: Timothy Fitz | last post by:
It seems to me that in python, generators are not truly coroutines. I do not understand why. What I see is that generators are used almost exclusively for generation of lists just-in-time. Side effects are frowned upon. Coroutines, in contrast, are like split functions where side effects are often as important or more important than return values. I am currently writing a real time strategy game where I have visual effects and use...
3
1533
by: Anupam Kapoor | last post by:
hi all, a python n00b, so please bear with me. i have a simple question: i generally name python sources as a-simple-python-example.py. when i try to import a module named as above, i (obviously) get tracebacks from python interpreter. is there a way to continue naming python sources as above, and still use it as python modules ? i can ofcourse change the name to
4
1432
by: turnstyle | last post by:
Hey all, sorry for asking such a grunt question, and thanks in advance for any help... My hosting ISP offers access to their MS-SQL database, and my understanding is that it's generally simplest to use some sort of client app to do stuff like adding tables and whatnot. SQL Server includes Enterprise Manager, but since my ISP is running the server, I don't need my own copy of SQL Server, just the client.
15
977
by: ECathell | last post by:
Ok I have been programming in vb.net since it was released. I love it and I have a great time seeing my code come to life. Recently I have been seeking to expand my horizons(ie looking for a new job). I see a lot of C# jobs...C# this C# that. So I think to myself, ok ill start learning C#. So I start working on programs that I already have done in vb.net and working on transitioning them to C#. I have come to the conclusion that intellisense...
11
3429
by: Deiter | last post by:
State Machines and Coroutines The other thread on goto: Lead me to want to ask... In the spirit of state machines and coroutines, This n00b to C would like to know if setjmp/longjmp are the only way to break out of a routine; leave the function and re-enter the stack state. I ask this because I've only briefly, as yet, reviewed these functions and I believe longjmp will only return a designated int. Therefore I would naively assume...
4
10146
by: onefry | last post by:
Hey I have this prog that i'm working on, starting my first c++ class and kind of a n00b to programming here it is #include <iostream> #include <cstdlib> using namespace std;
3
1328
by: rtlshred | last post by:
Hello I have just, just started C++ programing. the complier I am using is Dev C++ Here is my question: Once I have written some code, How do I run the program and see the output?
13
2010
by: Rosario | last post by:
do you like the "GetLine_m" function i have written today? How many errors do you see? i have try only the option for to load the all file in a string not the option to load a line "\n" ended so it should be full of errors #define P printf
0
1198
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 to `send´. You still can't suspend execution through arbitrary stack frames; `yield´ only...
0
9621
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10106
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.