This is a library for network oriented, coroutine based programming. The interfaces and events/operations aim to mimic some of the regular thread and socket features.
cogen uses the enhanced generators in python 2.5. These generators are bidirectional: they allow to pass values in and out of the generator. The whole framework is based on this.
The generator yields a Operation instance and will receive the result from that yield when the operation is ready.
Example:
@coroutine
def mycoro(bla):
yield <operation>
yield <operation>
Roughly the cogen internals works like this:
+------------------------+
| @coroutine |
| def foo(): |
| ... | op.process(sched, coro)
| +->result = yield op--|----------------+------------+
| | ... | | |
+--|---------------------+ +---------------+ +---------------------+
| | the operation | | the operation can't |
result = op.finalize() | is ready | | complete right now |
| +------|--------+ +----------|----------+
scheduler runs foo | |
| | |
foo gets in the active | |
coroutines queue | |
| | |
+----------------------<----------+ |
| depening on the op
op.run() +---------+
| socket is ready add it in | |
+-------------<------------ ...... the proactor <--+ |
| later |
+------<------------------- ...... add it in some other <-+
some event decides queue for later run
this op is ready
The coroutine decorator wrappes foo in a Coroutine class that does some niceties like exception handling, getting the result from finalize() etc.