Re:Co-routines as an alternative to state machines
by abecedarius on Saturday August 29, 2009 @03:55PM
(#793552)
Nice explanation, but the Python example could be expressed just as naturally, and with a bit less code, using a generator instead of a coroutine. I'd have added a note on when to use which style: prefer generators since Python supports them better, but use coroutines when the producer of the data stream has to be 'on top' in the control flow.
[ Parent
| on HN ]
Re:Co-routines as an alternative to state machines
by silentbicycle on Saturday August 29, 2009 @10:22PM
(#793955)
A "generator" is a subtype of coroutine. Python calls them "generators" because 1) that's what Icon called them, and 2) Python's implementation of coroutines is* restricted by being unable to yield from within called functions, among other things, so they're not as generally useful.
* Was? Maybe Python 3 fixed this. I use Lua instead now -- it's cleaner, consistently gets these things (TCO, lambdas, coroutines, the GIL, etc.) right, and I'm not going to wait forever for Guido to change his mind.
Re:Co-routines as an alternative to state machines
by abecedarius on Tuesday September 01, 2009 @12:46AM
(#797406)
I followed the language of Beazley's slides on Python coroutines: a 'generator' is a thing like this:
def g():
for i in range(10):
yield i
and a 'coroutine' is a thing like this: @coroutine
def c():
while True:
x = yield
print x
The code in the original post is like the latter. g() produces successive outputs when you call .next() on it; c() consumes successive inputs when you call .send(input) on it. The g() style has been in Python longer and is better integrated into the language -- you can say "for x in g():", for example. That's the point I was making, maybe too tersely.Your point 2 is still correct, though there's a PEP to make trampolining to work around it work nicer.
Re:Co-routines as an alternative to state machines
by cturner on Sunday August 30, 2009 @04:46AM
(#794258)
Yielding from within called functions is the basic generator syntax, but I think I know what you're talking about.
> Maybe Python 3 fixed this
Can you verify that this is what is being talked about at page 26 of this link? http://www.dabeaz.com/coroutines/Coroutines.pdf
If so - Python 2.5.
Re:Co-routines as an alternative to state machines
by silentbicycle on Sunday August 30, 2009 @08:06AM
(#794394)
Not slide or page 26, no.
From PEP 342:
Also, generators cannot yield control while other functions are
executing, unless those functions are themselves expressed as
generators, and the outer generator is written to yield in response
to values yielded by the inner generator. This complicates the
implementation of even relatively simple use cases like asynchronous
communications, because calling any functions either requires the
generator to "block" (i.e. be unable to yield control), or else a
lot of boilerplate looping code must be added around every needed
function call.
It looks like 2.5 fixed that particular issue. (I'm not holding my breath on tail-call optimization, though...)
Re:Co-routines as an alternative to state machines
by NateLawson on Saturday August 29, 2009 @07:39PM
(#793797)
Good point. I had to re-read the article a couple times to figure out why this wasn't implemented as a generator. The Dunkels' TCP stack approach in C made more sense.