Previous topic

cogen.core.coroutines

Next topic

cogen.core.pipe

This Page

Quick search

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

cogen.core.events

Base events (coroutine operations) and coroutine exceptions.

exception cogen.core.events.CoroutineException

Bases: exceptions.Exception

This is used intenally to carry exception state in the poller and scheduler.

exception cogen.core.events.ConnectionError

Bases: exceptions.Exception

Raised when a socket has a error flag (in epoll or select)

exception cogen.core.events.ConnectionClosed

Bases: exceptions.Exception

Raised when the other peer has closed connection.

exception cogen.core.events.OperationTimeout

Bases: exceptions.Exception

Raised when the timeout for a operation expires. The exception message will be the operation

class cogen.core.events.WaitForSignal(name, **kws)

Bases: cogen.core.events.TimedOperation

The coroutine will resume when the same object is Signaled.

Eg:

value = yield events.WaitForSignal(
    name, 
    timeout=None, 
    weak_timeout=True,
    prio=priority.DEFAULT
)
  • name - a object to wait on, can use strings for this - string used to wait is equal to the string used to signal.
  • value - a object sent with the signal. See: Signal

See: TimedOperation.

cleanup(sched, coro)
Remove this coro from the waiting for signal queue.
finalize()
name
process(sched, coro)
Add the calling coro in a waiting for signal queue.
result
class cogen.core.events.Signal(name, value=None, recipients=0, **kws)

Bases: cogen.core.events.Operation

This will resume the coroutines that where paused with WaitForSignal.

Usage:

nr = yield events.Signal(
    name, 
    value,
    prio=priority.DEFAULT
)
  • nr - the number of coroutines woken up
  • name - object that coroutines wait on, can be a string
  • value - object that the waiting coros recieve when they are resumed.

See: Operation.

coro
finalize()
len
name
prio
process(sched, coro)
If there aren’t enough coroutines waiting for the signal as the recipicient param add the calling coro in another queue to be activated later, otherwise activate the waiting coroutines.
recipients
result
value
cogen.core.events.Call(coro, args=None, kwargs=None, **kws)

You don’t need to use this. You can just yield the called coroutine:

result = yield mycoro( [arguments] )

Compared to OldCall, instead of returning an Operation object it returns the new Coroutine directly that will act as a Call operation in it’s pre-init state. This is faster for 2 reasons: avoids one Operation instatiation and avoids pushing and poping the new coroutine on the active coros queue.

class cogen.core.events.AddCoro(coro, args=None, kwargs=None, **kws)

Bases: cogen.core.events.Operation

A operation for adding a coroutine in the scheduler.

Example:

yield events.AddCoro(some_coro, args=(), kwargs={})

This is similar to Call, but it doesn’t pause the current coroutine. See: Operation.

args
coro
finalize()
Return a reference to the instance of the newly added coroutine.
kwargs
process(sched, coro)
Add the given coroutine in the scheduler.
result
class cogen.core.events.Join(coro, **kws)

Bases: cogen.core.events.TimedOperation

A operation for waiting on a coroutine.

Example:

@coroutine
def coro_a():
    return_value = yield events.Join(ref)
    
    
@coroutine
def coro_b():
    yield "bla"
    raise StopIteration("some return value")

ref = scheduler.add(coro_b)
scheduler.add(coro_a)

This will pause the coroutine and resume it when the other coroutine (ref in the example) has died.

cleanup(sched, coro)
Remove the calling coro from the waiting list.
coro
process(sched, coro)
Add the calling coroutine as a waiter in the coro we want to join. Also, doesn’t keep the called active (we’ll be activated back when the joined coro dies).
class cogen.core.events.Sleep(val)

Bases: cogen.core.events.TimedOperation

A operation to pausing the coroutine for a specified amount of time.

Usage:

yield events.Sleep(time_object)
  • time_object - a datetime or timedelta object, or a number of seconds
yield events.Sleep(timestamp=ts)
  • ts - a timestamp
cleanup(sched, coro)
process(sched, coro)
class cogen.core.events.Operation(prio=-1)

Bases: object

All operations derive from this. This base class handles the priority flag.

Eg:

yield Operation(prio=priority.DEFAULT)
  • prio - a priority constant, where the coro is appended on the active coroutine queue and how the coroutine is runned depend on this.

If you need something that can’t be done in a coroutine fashion you probabily need to subclass this and make a custom operation for your issue.

Note: you don’t really use this, this is for subclassing for other operations.

finalize()
Called just before the Coroutine wrapper passes the operation back in the generator. Return value is the value actualy sent in the generator. Subclasses might overwrite this method and call it from the superclass.
prio
process(sched, coro)
This is called when the operation is to be processed by the scheduler. Code here works modifies the scheduler and it’s usualy very crafty. Subclasses usualy overwrite this method and call it from the superclass.
state
class cogen.core.events.TimedOperation(timeout=None, weak_timeout=True, **kws)

Bases: cogen.core.events.Operation

Operations that have a timeout derive from this.

Eg:

yield TimedOperation(
    timeout=None, 
    weak_timeout=True,
    prio=priority.DEFAULT
)
  • timeout - can be a float/int (number of seconds) or a timedelta or a datetime value if it’s a datetime the timeout will occur on that moment
  • weak_timeout - strong timeouts just happen when specified, weak_timeouts get delayed if some action happens (eg: new but not enough data recieved)

See: Operation. Note: you don’t really use this, this is for subclassing for other operations.

cleanup(sched, coro)
Clean up after a timeout. Implemented in ops that need cleanup. If return value evaluated to false the sched won’t raise the timeout in the coroutine.
coro
delta
last_checkpoint
process(sched, coro)
Add the timeout in the scheduler, check for defaults.
set_timeout(val)
timeout
weak_timeout
class cogen.core.events.OldCall(coro, args=None, kwargs=None, **kws)

Bases: cogen.core.events.Operation

This will pause the current coroutine, add a new coro in the scheduler and resume the callee when it returns.

Usage:

result = yield events.Call(mycoro, args=(), kwargs={}, prio=priority.DEFAULT)
  • mycoro - the coroutine to add.
  • args, kwargs - params to call the coroutine with
  • if prio is set the new coroutine will be added in the top of the scheduler queue

See: Operation.

args
coro
kwargs
process(sched, coro)
Add the called coro in the sched and set the calling coroutine as the caller in the called coro (the called coro will activate the calling coro when it dies).