kazoo.handlers.gevent¶
A gevent based handler.
Public API¶
- class kazoo.handlers.gevent.SequentialGeventHandler[source]¶
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.
- async_result()[source]¶
Create a AsyncResult instance
The AsyncResult instance will have its completion callbacks executed in the thread the SequentialGeventHandler is created in (which should be the gevent/main thread).
- dispatch_callback(callback)[source]¶
Dispatch to the callback object
The callback is put on separate queues to run depending on the type as documented for the SequentialGeventHandler.
- 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.
If ref is false, the greenlet running sleep() will not prevent gevent.wait() from exiting.
Private API¶
- class kazoo.handlers.gevent.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() method is called. Waiters may receive the passed value or exception by calling get() method instead of wait(). An AsyncResult instance cannot be reset.
To pass a value call set(). Calls to get() (those that currently blocking as well as those made in the future) will return the value:
>>> result = AsyncResult() >>> result.set(100) >>> result.get() 100To pass an exception call set_exception(). This will cause get() to raise that exception:
>>> result = AsyncResult() >>> result.set_exception(RuntimeError('failure')) >>> result.get() Traceback (most recent call last): ... RuntimeError: failureAsyncResult implements __call__() and thus can be used as link() target:
>>> import gevent >>> result = AsyncResult() >>> gevent.spawn(lambda : 1/0).link(result) >>> result.get() Traceback (most recent call last): ... ZeroDivisionError: integer division or modulo by zero
- exception¶
Holds the exception instance passed to set_exception() if set_exception() was called. Otherwise None.
- get(block=True, timeout=None)¶
Return the stored value or raise the exception.
If this instance already holds a value / an exception, return / raise it immediatelly. Otherwise, block until another greenlet calls set() or set_exception() or until the optional timeout occurs.
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).
- get_nowait()¶
Return the value or raise the exception without blocking.
If nothing is available, raise gevent.Timeout immediatelly.
- rawlink(callback)¶
Register a callback to call when a value or an exception is set.
callback will be called in the Hub, so it must not use blocking gevent API. callback will be passed one argument: this instance.
- ready()¶
Return true if and only if it holds a value or an exception
- set(value=None)¶
Store the value. Wake up the waiters.
All greenlets blocking on get() or wait() are woken up. Sequential calls to wait() and get() will not block at all.
- set_exception(exception)¶
Store the exception. Wake up the waiters.
All greenlets blocking on get() or wait() are woken up. Sequential calls to wait() and get() will not block at all.
- successful()¶
Return true if and only if it is ready and holds a value
- wait(timeout=None)¶
Block until the instance is ready.
If this instance already holds a value / an exception, return immediatelly. Otherwise, block until another thread calls set() or set_exception() or until the optional timeout occurs.
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).
Return value.