| 1 |
.. _async_usage: |
| 2 |
|
| 3 |
================== |
| 4 |
Asynchronous Usage |
| 5 |
================== |
| 6 |
|
| 7 |
The asynchronous Kazoo API relies on the |
| 8 |
:class:`~kazoo.interfaces.IAsyncResult` object which is returned by all the |
| 9 |
asynchronous methods. Callbacks can be added with the |
| 10 |
:meth:`~kazoo.interfaces.IAsyncResult.rawlink` method which works in a |
| 11 |
consistent manner whether threads or an asynchronous framework like gevent is |
| 12 |
used. |
| 13 |
|
| 14 |
Kazoo utilizes a pluggable :class:`~kazoo.interfaces.IHandler` interface which |
| 15 |
abstracts the callback system to ensure it works consistently. |
| 16 |
|
| 17 |
Connection Handling |
| 18 |
=================== |
| 19 |
|
| 20 |
Creating a connection: |
| 21 |
|
| 22 |
.. code-block:: python |
| 23 |
|
| 24 |
from kazoo.client import KazooClient |
| 25 |
from kazoo.handlers.gevent import SequentialGeventHandler |
| 26 |
|
| 27 |
zk = KazooClient(handler=SequentialGeventHandler()) |
| 28 |
|
| 29 |
# returns immediately |
| 30 |
event = zk.start_async() |
| 31 |
|
| 32 |
# Wait for 30 seconds and see if we're connected |
| 33 |
event.wait(timeout=30) |
| 34 |
|
| 35 |
if not zk.connected: |
| 36 |
# Not connected, stop trying to connect |
| 37 |
zk.stop() |
| 38 |
raise Exception("Unable to connect.") |
| 39 |
|
| 40 |
In this example, the `wait` method is used on the event object returned by the |
| 41 |
:meth:`~kazoo.client.KazooClient.start_async` method. A timeout is **always** |
| 42 |
used because its possible that we might never connect and that should be |
| 43 |
handled gracefully. |
| 44 |
|
| 45 |
The :class:`~kazoo.handlers.gevent.SequentialGeventHandler` is used when you |
| 46 |
want to use gevent (and |
| 47 |
:class:`~kazoo.handlers.eventlet.SequentialEventletHandler` when eventlet is |
| 48 |
used). Kazoo doesn't rely on gevents/eventlet monkey patching and requires |
| 49 |
that you pass in the appropriate handler, the default handler is |
| 50 |
:class:`~kazoo.handlers.threading.SequentialThreadingHandler`. |
| 51 |
|
| 52 |
Asynchronous Callbacks |
| 53 |
====================== |
| 54 |
|
| 55 |
All kazoo `_async` methods except for |
| 56 |
:meth:`~kazoo.client.KazooClient.start_async` return an |
| 57 |
:class:`~kazoo.interfaces.IAsyncResult` instance. These instances allow |
| 58 |
you to see when a result is ready, or chain one or more callback |
| 59 |
functions to the result that will be called when it's ready. |
| 60 |
|
| 61 |
The callback function will be passed the |
| 62 |
:class:`~kazoo.interfaces.IAsyncResult` instance and should call the |
| 63 |
:meth:`~kazoo.interfaces.IAsyncResult.get` method on it to retrieve |
| 64 |
the value. This call could result in an exception being raised |
| 65 |
if the asynchronous function encountered an error. It should be caught |
| 66 |
and handled appropriately. |
| 67 |
|
| 68 |
Example: |
| 69 |
|
| 70 |
.. code-block:: python |
| 71 |
|
| 72 |
import sys |
| 73 |
|
| 74 |
from kazoo.exceptions import ConnectionLossException |
| 75 |
from kazoo.exceptions import NoAuthException |
| 76 |
|
| 77 |
def my_callback(async_obj): |
| 78 |
try: |
| 79 |
children = async_obj.get() |
| 80 |
do_something(children) |
| 81 |
except (ConnectionLossException, NoAuthException): |
| 82 |
sys.exit(1) |
| 83 |
|
| 84 |
# Both these statements return immediately, the second sets a callback |
| 85 |
# that will be run when get_children_async has its return value |
| 86 |
async_obj = zk.get_children_async("/some/node") |
| 87 |
async_obj.rawlink(my_callback) |
| 88 |
|
| 89 |
Zookeeper CRUD |
| 90 |
============== |
| 91 |
|
| 92 |
The following CRUD methods all work the same as their synchronous counterparts |
| 93 |
except that they return an :class:`~kazoo.interfaces.IAsyncResult` object. |
| 94 |
|
| 95 |
Creating Method: |
| 96 |
|
| 97 |
* :meth:`~kazoo.client.KazooClient.create_async` |
| 98 |
|
| 99 |
Reading Methods: |
| 100 |
|
| 101 |
* :meth:`~kazoo.client.KazooClient.exists_async` |
| 102 |
* :meth:`~kazoo.client.KazooClient.get_async` |
| 103 |
* :meth:`~kazoo.client.KazooClient.get_children_async` |
| 104 |
|
| 105 |
Updating Methods: |
| 106 |
|
| 107 |
* :meth:`~kazoo.client.KazooClient.set_async` |
| 108 |
|
| 109 |
Deleting Methods: |
| 110 |
|
| 111 |
* :meth:`~kazoo.client.KazooClient.delete_async` |
| 112 |
|
| 113 |
The :meth:`~kazoo.client.KazooClient.ensure_path` has no asynchronous |
| 114 |
counterpart at the moment nor can the |
| 115 |
:meth:`~kazoo.client.KazooClient.delete_async` method do recursive deletes. |