A gevent based handler.
- class kazoo.handlers.gevent.SequentialGeventHandler¶
Gevent handler for sequentially executing callbacks.
This handler executes callbacks in a sequential manner. A queue is created for each of the callback events, so that each type of event has its callback type run sequentially.
Each queue type has a greenlet worker that pulls the callback event off the queue and runs it in the order the client sees it.
This split helps ensure that watch callbacks won’t block session re-establishment should the connection be lost during a Zookeeper client call.
Watch callbacks should avoid blocking behavior as the next callback of that type won’t be run until it completes. If you need to block, spawn a new greenlet and return immediately so callbacks can proceed.
Create a AsyncResult instance
Dispatch to the callback object
The callback is put on separate queues to run depending on the type as documented for the SequentialGeventHandler.
Create an appropriate Event object
Create an appropriate Lock object
alias of Empty
alias of Queue
Create an appropriate RLock object
- static sleep_func(seconds=0, ref=True)¶
Put the current greenlet to sleep for at least seconds.
seconds may be specified as an integer, or a float if fractional seconds are desired.
In the current implementation, a value of 0 (the default) means to yield execution to any other runnable greenlets, but this greenlet may be scheduled again before the event loop cycles (in an extreme case, a greenlet that repeatedly sleeps with 0 can prevent greenlets that are ready to do I/O from being scheduled for some (small) period of time); a value greater than 0, on the other hand, will delay running this greenlet until the next iteration of the loop.
If ref is False, the greenlet running sleep() will not prevent gevent.wait() from exiting.
Changed in version 1.3a1: Sleeping with a value of 0 will now be bounded to approximately block the loop for no longer than gevent.getswitchinterval().
- spawn(func, *args, **kwargs)¶
Spawn a function to run asynchronously
Start the greenlet workers.
Stop the greenlet workers and empty all queues.
- class kazoo.handlers.gevent.AsyncResult¶
AsyncResult() A one-time event that stores a value or an exception.
Like Event it wakes up all the waiters when set() or set_exception() is called. Waiters may receive the passed value or exception by calling get() instead of wait(). An AsyncResult instance cannot be reset.>>> result = AsyncResult() >>> result.set(100) >>> result.get() 100>>> result = AsyncResult() >>> result.set_exception(RuntimeError('failure')) >>> result.get() Traceback (most recent call last): ... RuntimeError: failure
AsyncResult implements __call__() and thus can be used as link() target:>>> import gevent >>> result = AsyncResult() >>> gevent.spawn(lambda : 1/0).link(result) >>> try: ... result.get() ... except ZeroDivisionError: ... print('ZeroDivisionError') ZeroDivisionError
The order and timing in which waiting greenlets are awakened is not determined. As an implementation note, in gevent 1.1 and 1.0, waiting greenlets are awakened in a undetermined order sometime after the current greenlet yields to the event loop. Other greenlets (those not waiting to be awakened) may run between the current greenlet yielding and the waiting greenlets being awakened. These details may change in the future.
Changed in version 1.1: The exact order in which waiting greenlets are awakened is not the same as in 1.0.
Changed in version 1.1: Callbacks linked to this object are required to be hashable, and duplicates are merged.
- cancel(self) → bool¶
- cancelled(self) → bool¶
- done(self) → bool¶
- get(self, block=True, timeout=None)¶
Return the stored value or raise the exception.
When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof). If the timeout elapses, the Timeout exception will be raised.
Parameters: block (bool) – If set to False and this instance is not ready, immediately raise a Timeout exception.
Return the value or raise the exception without blocking.
If this object is not yet ready, raise gevent.Timeout immediately.
- ready(self) → bool¶
Return true if and only if it holds a value or an exception
- result(self, timeout=None)¶
- set(self, value=None)¶
Store the value and wake up any waiters.
- set_exception(self, exception, exc_info=None)¶
Store the exception and wake up any waiters.
Parameters: exc_info (tuple) – If given, a standard three-tuple of type, value, traceback as returned by sys.exc_info(). This will be used when the exception is re-raised to propagate the correct traceback.
AsyncResult.set(self, value=None) Store the value and wake up any waiters.
- successful(self) → bool¶
Return true if and only if it is ready and holds a value
- wait(self, timeout=None)¶
Block until the instance is ready.
If this instance already holds a value, it is returned immediately. If this instance already holds an exception, None is returned immediately.
Otherwise, block until another greenlet calls set() or set_exception() (at which point either the value or None will be returned, respectively), or until the optional timeout expires (at which point None will also be returned).
When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).
If a timeout is given and expires, None will be returned (no timeout exception will be raised).