473,396 Members | 1,797 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,396 software developers and data experts.

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_pointer = (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 1252
On Sep 12, 8:08*pm, i...@illusorygoals.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_pointer = (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...@illusorygoals.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_pointer = (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...@illusorygoals.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_pointer = (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
"cogenerators", 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...@illusorygoals.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
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...
3
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...
4
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...
15
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...
11
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...
4
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
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
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...
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´...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.