Previous topic

cogen.core

Next topic

cogen.core.events

This Page

Quick search

Enter search terms or a module, class or function name.

cogen.core.coroutines

Coroutine related boilerplate and wrappers.

class cogen.core.coroutines.local

Bases: object

A threadlocal-like object that works in the context of coroutines. That means, the current running coroutine has the _ident_.

Coroutine.run_op sets the indent before running a step and unsets after.

Example:

loc = local() 
loc.foo = 1

The loc instance’s values will be different for separate coroutines.

cogen.core.coroutines.coro
alias of Coroutine
class cogen.core.coroutines.CoroutineInstance(coro, *args, **kws)

Bases: cogen.core.events.Operation

We need a coroutine wrapper for generators and functions alike because we want to run functions that don’t return generators just like a coroutine, also, we do some exception handling here.

add_waiter(coro, op=None)
caller
coro
debug
exception
f_args
f_kws
finalize()
handle_error()
lastop
name
prio
process(sched, coro)
remove_waiter(coro, op=None)
result
run_op(op)

Handle the operation:

  • if coro is in STATE_RUNNING, send or throw the given op
  • if coro is in STATE_NEED_INIT, call the init function and if it doesn’t return a generator, set STATE_COMPLETED and set the result to whatever the function returned.
    • if StopIteration is raised, set STATE_COMPLETED and return self.
    • if any other exception is raised, set STATE_FAILED, handle error or send it to the caller, return self

Return self is used as a optimization. Coroutine is also a Operation which handles it’s own completion (resuming the caller and the waiters).

running
state
waiters