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
.
queue_empty
¶alias of
Queue.Empty
queue_impl
¶alias of
gevent._gevent_cqueue.Queue
- 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.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()
.See also
idle()
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 whenset()
orset_exception()
is called. Waiters may receive the passed value or exception by callingget()
instead ofwait()
. AnAsyncResult
instance cannot be reset.Important
This object is for communicating among greenlets within the same thread only! Do not try to use it to communicate across threads.
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:>>> from gevent.event import AsyncResult >>> 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') ZeroDivisionErrorNote
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.Changed in version 1.5a3: Waiting greenlets are now awakened in the order in which they waited.
Changed in version 1.5a3: The low-level
rawlink
method (most users won’t use this) now automatically unlinks waiters before calling them.
cancel
(self) → bool¶
cancelled
(self) → bool¶
done
(self) → bool¶
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
(self, 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 immediately. 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). 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 aTimeout
exception.
get_nowait
(self)¶Return the value or raise the exception without blocking.
If this object is not yet
ready
, raisegevent.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.
All greenlets blocking on
get()
orwait()
are awakened. Subsequent calls towait()
andget()
will not block at all.
set_exception
(self, exception, exc_info=None)¶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.
set_result
()¶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()
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).