Coroutines In Python

A coroutine is a generalization of the subroutine. Forget for a moment everything you've been taught about calling functions, and stacks, etc... Think back to BASIC, and the evil GOTO statement. Imagine a more powerful GOTO that could jump back and forth between functions, passing arguments, and you begin to get the idea

Coroutines are a bit of ancient forgotten computer-science lore; stamped out of the collective memory by the hegemony of C. But they are useful in a wide variety of situations that can only be clumsily solved using 'standard' tools like threads and processes.

Coroutines can be used to simplify just about any difficult state-machine programming problem. Any time you find yourself tempted to build a complex state machine to solve what should be a simple problem, you've actually been pining for coroutines. With them, you can usually turn an 'inside-out' problem into a 'right-side-out' problem, and replace a few pages of hairy code with a single function.

A popular application of coroutines is in the construction of lightweight threading libraries (in fact, on Win32 coroutines are called 'fibers'). Coroutine threads are more scalable than OS threads - a desktop machine can easily juggle tens of thousands of them without gobbling up the entire virtual memory space.

Efficient coroutines hold out the possibility of building powerful, stateful internet servers that are massively scalable.

python-coro eGroup

Coroutines are Cool. Discuss.
Subscribe to python-coro
Powered by www.egroups.com

Stackless Python

Coroutines in a Virgin Python

Combining I/O Event-Delivery with Coroutines

If you can convince your OS to deliver events, you can avoid the overhead of select() and poll() entirely...


Last modified: Tue Jul 25 03:53:33 PDT 2000