kazoo.interfaces¶
Kazoo Interfaces
Changed in version 1.4: The classes in this module used to be interface declarations based on zope.interface.Interface. They were converted to normal classes and now serve as documentation only.
Public API¶
IHandler implementations should be created by the developer to be
passed into KazooClient during instantiation for the
preferred callback handling.
If the developer needs to use objects implementing the IAsyncResult
interface, the IHandler.async_result() method must be used instead of
instantiating one directly.
- class kazoo.interfaces.IHandler(*args, **kwargs)[source]¶
A Callback Handler for Zookeeper completion and watch callbacks.
This object must implement several methods responsible for determining how completion / watch callbacks are handled as well as the method for calling
IAsyncResultcallback functions.These functions are used to abstract differences between a Python threading environment and asynchronous single-threaded environments like gevent. The minimum functionality needed for Kazoo to handle these differences is encompassed in this interface.
The Handler should document how callbacks are called for:
Zookeeper completion events
Zookeeper watch events
- name¶
Human readable name of the Handler interface.
- timeout_exception¶
Exception class that should be thrown and captured if a result is not available within the given time.
- sleep_func¶
Appropriate sleep function that can be called with a single argument and sleep.
- async_result()[source]¶
Return an instance that conforms to the
IAsyncResultinterface appropriate for this handler
- Return type:
- create_connection(*args, **kwargs)[source]¶
A socket method that implements Python’s socket.create_connection API
- Return type:
Socket
- create_socket_pair()[source]¶
A socket method that implements Python’s socket.socketpair API
- Return type:
tuple[Socket,Socket]
- event_object()[source]¶
Return an appropriate object that implements Python’s threading.Event API
- Return type:
Event
- lock_object()[source]¶
Return an appropriate object that implements Python’s threading.Lock API
- Return type:
Lockable
- rlock_object()[source]¶
Return an appropriate object that implements Python’s threading.RLock API
- Return type:
ReentrantLock
- select(rlist, wlist, xlist, timeout=None)[source]¶
A select method that implements Python’s select.select API
- Return type:
tuple[Iterable[int|HasFileNo],Iterable[int|HasFileNo],Iterable[int|HasFileNo]]
- spawn(func, *args, **kwargs)[source]¶
Spawn a function to run asynchronously
- Parameters:
args (
Any) – args to call the function with.kwargs (
Any) – keyword args to call the function with.- Return type:
ThreadlikeThis method should return immediately and execute the function with the provided args and kwargs in an asynchronous manner.
Private API¶
The IAsyncResult documents the proper implementation for providing
a value that results from a Zookeeper completion callback. Since the
KazooClient returns an IAsyncResult object
instead of taking a completion callback for async functions, developers
wishing to have their own callback called should use the
IAsyncResult.rawlink() method.
- class kazoo.interfaces.IAsyncResult(*args, **kwargs)[source]¶
An Async Result object that can be queried for a value that has been set asynchronously.
This object is modeled on the
geventAsyncResult object.The implementation must account for the fact that the
set()andset_exception()methods will be called from within the Zookeeper thread which may require extra care under asynchronous environments.
- exception¶
Holds the exception instance passed to
set_exception()ifset_exception()was called. Otherwise None.
- property exception: Exception | None¶
The exception set by
set_exception()or None if no exception has been set
- get(block=True, timeout=None)[source]¶
Return the stored value or raise the exception
- Parameters:
block (
bool) – Whether this method should block or return immediately.timeout (
float|None) – How long to wait for a value when block is True.- Return type:
AnyIf this instance already holds a value / an exception, return / raise it immediately. Otherwise, block until
set()orset_exception()has been called or until the optional timeout occurs.
- get_nowait()[source]¶
Return the value or raise the exception without blocking.
If nothing is available, raise the Timeout exception class on the associated
IHandlerinterface.
- Return type:
Any
- rawlink(callback)[source]¶
Register a callback to call when a value or an exception is set
- Parameters:
callback (
Callable[[IAsyncResult],Any]) – A callback function to call afterset()orset_exception()has been called. This function will be passed a single argument, this instance.- Return type:
None
- set(value=None)[source]¶
Store the value. Wake up the waiters.
- Parameters:
value (
Any) – Value to store as the result.- Return type:
NoneAny waiters blocking on
get()orwait()are woken up. Sequential calls towait()andget()will not block at all.
- set_exception(exception)[source]¶
Store the exception. Wake up the waiters.
- Parameters:
exception (
Exception) – Exception to raise when fetching the value.- Return type:
NoneAny waiters blocking on
get()orwait()are woken up. Sequential calls towait()andget()will not block at all.
- unlink(callback)[source]¶
Remove the callback set by
rawlink()
- Parameters:
callback (
Callable[[IAsyncResult],None]) – A callback function to remove.- Return type:
None
- wait(timeout=None)[source]¶
Block until the instance is ready.
- Parameters:
timeout (
float|None) – How long to wait for a value when block is True.- Return type:
AnyIf this instance already holds a value / an exception, return / raise it immediately. Otherwise, block until
set()orset_exception()has been called or until the optional timeout occurs.