Most "active" coroutine library project?

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Wed Sep 23 16:50:08 EDT 2009


On 08:16 pm, sajmikins at gmail.com wrote:
>On Wed, Sep 23, 2009 at 2:05 PM,  <exarkun at twistedmatrix.com> wrote:
>[snip]
>>
>>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.)

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



More information about the Python-list mailing list