Module: cogen.core.events
Base events (coroutine operations) and coroutine exceptions.
Classes
AddCoro
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.
ConnectionClosed
Raised when the other peer has closed connection.
ConnectionError
Raised when a socket has a error flag (in epoll or select)
CoroutineException
This is used intenally to carry exception state in the poller and scheduler.
Join
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.
OldCall
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.
Operation
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.
OperationTimeout
Raised when the timeout for a operation expires. The exception message will be the operation
Signal
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.
Sleep
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
TimedOperation
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.
WaitForSignal
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.
Functions
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.