Most "active" coroutine library project?

P

Phillip B Oldham

I've been taking a look at the multitude of coroutine libraries
available for Python, but from the looks of the projects they all seem
to be rather "quiet". I'd like to pick one up to use on a current
project but can't deduce which is the most popular/has the largest
community.

Libraries I looked at include: cogen, weightless, eventlet and
circuits (which isn't exactly coroutine-based but it's event-driven
model was intriguing).

Firstly, are there any others I've missed? And what would the
consensus be on the which has the most active community behind it?
 
M

Matthew Woodcraft

Phillip B Oldham said:
I've been taking a look at the multitude of coroutine libraries
available for Python, but from the looks of the projects they all seem
to be rather "quiet". I'd like to pick one up to use on a current
project but can't deduce which is the most popular/has the largest
community.

Libraries I looked at include: cogen, weightless, eventlet and
circuits (which isn't exactly coroutine-based but it's event-driven
model was intriguing).

Firstly, are there any others I've missed?

There's greenlets

http://pypi.python.org/pypi/greenlet

which I think is also fairly described as "quiet".

-M-
 
S

Simon Forman

I've been taking a look at the multitude of coroutine libraries
available for Python, but from the looks of the projects they all seem
to be rather "quiet". I'd like to pick one up to use on a current
project but can't deduce which is the most popular/has the largest
community.

Libraries I looked at include: cogen, weightless, eventlet and
circuits (which isn't exactly coroutine-based but it's event-driven
model was intriguing).

Firstly, are there any others I've missed? And what would the
consensus be on the which has the most active community behind it?

Coroutines are built into the language. There's a good talk about
them here: http://www.dabeaz.com/coroutines/

HTH,
~Simon
 
S

Simon Forman

But what some Python programmers call coroutines aren't really the same as
what the programming community at large would call a coroutine.

Jean-Paul

Really? I'm curious as to the differences. (I just skimmed the entry
for coroutines in Wikipedia and PEP 342, but I'm not fully
enlightened.)

Warm regards,
~Simon
 
G

Grant Edwards

Really? I'm curious as to the differences.

Me too. I read through the presentation above, it it seems to
describe pretty much exactly what we called co-routines both in
school and in the workplace.

Back when I worked on one of the first hand-held cellular
mobile phones, it used co-routines where the number of
coroutines was fixed at 2 (one for each register set in a Z80
CPU). The semantics seem to be identical to the coroutines
described in the presentation.
 
E

exarkun

Really? I'm curious as to the differences. (I just skimmed the entry
for coroutines in Wikipedia and PEP 342, but I'm not fully
enlightened.)

The important difference is that coroutines can switch across multiple
stack frames. Python's "enhanced generators" can still only switch
across one stack frame - ie, from inside the generator to the frame
immediately outside the generator. This means that you cannot use
"enhanced generators" to implement an API like this one:

def doSomeNetworkStuff():
s = corolib.socket()
s.connect(('google.com', 80))
s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n')
response = s.recv(8192)

where connect, sendall, and recv don't actually block the entire calling
thread, they only switch away to another coroutine until the underlying
operation completes. With "real" coroutines, you can do this.

Jean-Paul
 
G

Grant Edwards

The important difference is that coroutines can switch across
multiple stack frames. Python's "enhanced generators" can
still only switch across one stack frame - ie, from inside the
generator to the frame immediately outside the generator.

Good point. Being unable to "yeild" from inside a function
called by the "main" coroutine can be limiting once you try to
do something non-trivial. I had read about that limitation,
but had forgotten it.

Some "stackless" threading schemes for C (e.g. Protothreads
http://www.sics.se/~adam/pt/) have the same issue, and it does
require some extra effort to work around that limitation.
 
J

Jason Tackaberry

immediately outside the generator. This means that you cannot use
"enhanced generators" to implement an API like this one:

def doSomeNetworkStuff():
s = corolib.socket()
s.connect(('google.com', 80))
s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n')
response = s.recv(8192)

where connect, sendall, and recv don't actually block the entire calling
thread, they only switch away to another coroutine until the underlying
operation completes. With "real" coroutines, you can do this.

I might be missing some subtlety of your point, but I've implemented
this functionality using generators in a library called Kaa[1]. In kaa,
your example looks like:

import kaa

@kaa.coroutine()
def do_some_network_stuff():
s = kaa.Socket()
yield s.connect('google.com:80')
yield s.write('GET / HTTP/1.1\nHost: www.google.com\n\n')
response = yield s.read()

do_some_network_stuff()
kaa.main.run()


Of course, it does require a "coroutine scheduler" be implemented,
which, in kaa, is taken care of by the main loop.

Cheers,
Jason.

[1] The curious can visit http://doc.freevo.org/api/kaa/base/ and
http://freevo.org/kaa/ although a 1.0 hasn't yet been released and the
docs are still rather sketchy.
 
E

exarkun

immediately outside the generator. This means that you cannot use
"enhanced generators" to implement an API like this one:

def doSomeNetworkStuff():
s = corolib.socket()
s.connect(('google.com', 80))
s.sendall('GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n')
response = s.recv(8192)

where connect, sendall, and recv don't actually block the entire
calling
thread, they only switch away to another coroutine until the
underlying
operation completes. With "real" coroutines, you can do this.

I might be missing some subtlety of your point, but I've implemented
this functionality using generators in a library called Kaa[1]. In
kaa,
your example looks like:

import kaa

@kaa.coroutine()
def do_some_network_stuff():
s = kaa.Socket()
yield s.connect('google.com:80')
yield s.write('GET / HTTP/1.1\nHost: www.google.com\n\n')
response = yield s.read()

do_some_network_stuff()
kaa.main.run()

I specifically left out all "yield" statements in my version, since
that's exactly the point here. :) With "real" coroutines, they're not
necessary - coroutine calls look just like any other call. With
Python's enhanced generators, they are.

Jean-Paul
 
J

Jason Tackaberry

I specifically left out all "yield" statements in my version, since
that's exactly the point here. :) With "real" coroutines, they're not
necessary - coroutine calls look just like any other call. With
Python's enhanced generators, they are.

Yes, I take your point.

Explicitly yielding may not be such a terrible thing, though. It's more
clear when reading such code where it may return back to the scheduler.
This may be important for performance, unless of course the coroutine
implementation supports preemption.

Cheers,
Jason.
 
E

exarkun

Yes, I take your point.

Explicitly yielding may not be such a terrible thing, though. It's
more
clear when reading such code where it may return back to the scheduler.
This may be important for performance, unless of course the coroutine
implementation supports preemption.

Sure, no value judgement intended, except on the practice of taking
words with well established meanings and re-using them for something
else ;)

Jean-Paul
 
J

Jason Tackaberry

Sure, no value judgement intended, except on the practice of taking
words with well established meanings and re-using them for something
else ;)

I think it's the behaviour that's important, and not the specific syntax
needed to implement that behaviour.

In other words, I disagree (if this is what you're suggesting) that
sticking "yield" in front of certain expressions makes it any less a
coroutine.

Now, requiring explicit yields does mean that the coroutine has
specific, well-defined points of reentry. But I don't believe it's a
necessary condition that coroutines allow arbitrary (in the
non-deterministic sense) reentry points, only multiple.

Cheers,
Jason.
 
A

Antoine Pitrou

Grant Edwards said:
Back when I worked on one of the first hand-held cellular
mobile phones, it used co-routines where the number of
coroutines was fixed at 2 (one for each register set in a Z80
CPU).

Gotta love the lightning-fast EXX instruction. :)

Regards

Antoine.
 
M

Michele Simionato

I've been taking a look at the multitude of coroutine libraries
available for Python, but from the looks of the projects they all seem
to be rather "quiet". I'd like to pick one up to use on a current
project but can't deduce which is the most popular/has the largest
community.

Libraries I looked at include: cogen, weightless, eventlet and
circuits (which isn't exactly coroutine-based but it's event-driven
model was intriguing).

Firstly, are there any others I've missed? And what would the
consensus be on the which has the most active community behind it?

The newest one seems to be diesel, by Simon Willison & Co: http://dieselweb..org/lib
 
H

Hendrik van Rooyen

Gotta love the lightning-fast EXX instruction. :)

Using it in the above context is about equivalent to slipping a hand grenade
in amongst the other eggs in a nest.

:)

- Hendrik
 
G

Grant Edwards

Using it in the above context is about equivalent to slipping
a hand grenade in amongst the other eggs in a nest.

EXX accomplised much of the context switch operation. I don't
remember how much RAM was available, but it wasn't much...
 
P

Piet van Oostrum

e> I specifically left out all "yield" statements in my version, since that's
e> exactly the point here. :) With "real" coroutines, they're not necessary -
e> coroutine calls look just like any other call. With Python's enhanced
e> generators, they are.

The first time I encountered coroutines was in Simula-67. Coroutine
switching was certainly explicit there. IIRC, the keyword was resume.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,371
Latest member
TroyHursey

Latest Threads

Top