Microthreads without Stackless?

Bryan Olson fakeaddress at nowhere.org
Fri Sep 17 04:04:25 EDT 2004


David Mertz, Ph.D. wrote:
 > It's too bad you didn't bother to READ my article at:
 >
 >   http://gnosis.cx/publish/programming/charming_python_b5.html
 >
 > This is distinct from my other article that covers "weightless
 > threads", though there is some overlap in the concepts.

I spent a couple hours going through the "weightless threads"
paper, and looking up the background including that one.  I had
previously concluded that Python generators could not implement
what I wanted from real co-routines, so I was interested in
seeing if there was a reasonable implementation.

 > While you do need a scheduler to control the branching, once you have
 > this you get EXACTLY the same thing as coroutines in other languages.
 > Specifically, you can branch from any generator, into the body of
 > whatever other generator you wish.

Great; I'd love to have the same thing as real co-routines.
Here's the problem:  I have a server that currently handles
multiple clients using one thread per connection.  When a
client-handler needs to send or receive data, it simply reads or
writes to a file-like thing.

Now I want to handle twenty thousand clients, so I want to
replace my threads with co-routines.  With real co-routines, I
can easily do that.  I write an asynchronous I/O handler routine
that can wait on many files at once, perhaps with os.select().
I over-ride the file read and write procedures to initiate the
I/O, then yield.  When I/O is ready on a file, the I/O handler
can switch back to the client-handler.

Importantly, I do *not* have to re-write all the functions in
every call chain that leads to a read or write.  I just need the
I/O handler (which includes a kind of scheduler) at the top, and
I over-ride my file I/O calls at the bottom.

After reading your articles I am unable to see how generators,
even with your scheduler, offer any such capability.


-- 
--Bryan



More information about the Python-list mailing list