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
instanceThe
AsyncResult
instance will have its completion callbacks executed in the thread theSequentialGeventHandler
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.
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 preventgevent.wait()
from exiting.See also
idle()
Private API¶
- class
kazoo.handlers.gevent.
AsyncResult
[source]¶A one-time event that stores a value or an exception.
Like
Event
it wakes up all the waiters whenset()
orset_exception()
is called. Waiters may receive the passed value or exception by callingget()
instead ofwait()
. AnAsyncResult
instance cannot be reset.To pass a value call
set()
. Calls toget()
(those that are 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 causeget()
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 aslink()
target:>>> import gevent >>> result = AsyncResult() >>> gevent.spawn(lambda : 1/0).link(result) >>> try: ... result.get() ... except ZeroDivisionError: ... print('ZeroDivisionError') ZeroDivisionError
exc_info
¶The three-tuple of exception information if
set_exception()
was called.
exception
¶Holds the exception instance passed to
set_exception()
ifset_exception()
was called. OtherwiseNone
.
get
(block=True, timeout=None)[source]¶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()
orset_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).
Parameters: block (bool) – If set to False
and this instance is not ready, immediately raise aTimeout
exception.
get_nowait
()[source]¶Return the value or raise the exception without blocking.
If this object is not yet
ready
, raisegevent.Timeout
immediately.
rawlink
(callback)[source]¶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.
set
(value=None)[source]¶Store the value and wake up any waiters.
All greenlets blocking on
get()
orwait()
are awakened. Subsequent calls towait()
andget()
will not block at all.
set_exception
(exception, exc_info=None)[source]¶Store the exception and wake up any waiters.
All greenlets blocking on
get()
orwait()
are awakened. Subsequent calls towait()
andget()
will not block at all.
Parameters: exc_info (tuple) – If given, a standard three-tuple of type, value, traceback
as returned bysys.exc_info()
. This will be used when the exception is re-raised to propagate the correct traceback.
wait
(timeout=None)[source]¶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()
orset_exception()
(at which point either the value orNone
will be returned, respectively), or until the optional timeout expires (at which pointNone
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).