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.

event_object()[source]

Create an appropriate Event object

lock_object()[source]

Create an appropriate Lock object

rlock_object()[source]

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.

Tip

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.

See also

idle()

spawn(func, *args, **kwargs)[source]

Spawn a function to run asynchronously

start()[source]

Start the greenlet workers.

stop()[source]

Stop the greenlet workers and empty all queues.

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() is called. Waiters may receive the passed value or exception by calling get() instead of wait(). An AsyncResult instance cannot be reset.

To pass a value call set(). Calls to get() (those that are currently blocking as well as those made in the future) will return the value:

>>> result = AsyncResult()
>>> result.set(100)
>>> result.get()
100

To 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: 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

Note

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.

exc_info

The three-tuple of exception information if set_exception() was called.

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 or an exception, return or 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). 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.
get_nowait()

Return the value or raise the exception without blocking.

If this object is not yet ready, raise gevent.Timeout immediately.

ready()

Return true if and only if it holds a value or an exception

set(value=None)

Store the value and wake up any waiters.

All greenlets blocking on get() or wait() are awakened. Subsequent calls to wait() and get() will not block at all.

set_exception(exception, exc_info=None)

Store the exception and wake up any waiters.

All greenlets blocking on get() or wait() are awakened. Subsequent calls to wait() and get() will not block at all.

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.
set_result(value=None)

Store the value and wake up any waiters.

All greenlets blocking on get() or wait() are awakened. Subsequent calls to wait() and get() will not block at all.

successful()

Return true if and only if it is ready and holds a value

value

Holds the value passed to set() if set() was called. Otherwise, None

wait(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).

Note

If a timeout is given and expires, None will be returned (no timeout exception will be raised).