Apache ZooKeeper is shipped with an official client binding for Python, which is developed on top of the C bindings. It can be found in the contrib/zkpython
directory of the ZooKeeper distribution. To build and install the Python binding, refer to the instructions in the README
file there. In this section, we will learn about another popular Python client library for ZooKeeper, called
Kazoo (https://kazoo.readthedocs.org/).
Kazoo is a pure Python library for ZooKeeper, which means that, unlike the official Python bindings, Kazoo is implemented fully in Python and has no dependency on the C bindings of ZooKeeper. Along with providing both synchronous and asynchronous APIs, the Kazoo library also provides APIs for some distributed data structure primitives such as distributed locks, leader election, distributed queues, and so on.
Installation of Kazoo is very simple, which can be done either with pip
or easy_install
installers:
Using pip
, Kazoo can be installed with the following command:
$ pip install kazoo
Using easy_install
, Kazoo is installed as follows:
$ easy_install kazoo
To verify whether Kazoo is installed properly, let's try to connect to the ZooKeeper instance and print the list of znodes in the root path of the tree, as shown in the following screenshot:
In the preceding example, we imported the KazooClient
, which is the main ZooKeeper client class. Then, we created an object of the class (an instance of KazooClient) by connecting to the ZooKeeper instance that is running on the localhost. Once we called the start()
method, it initiates a connection to the ZooKeeper server.
Once successfully connected, the instance contains the handle to the ZooKeeper session. Now, when we called the get_children()
method on the root path of the ZooKeeper namespace, it returned a list of the children. Finally, we closed the connection by calling the stop()
method.
Detailed documentation of the Kazoo Python library is available at https://kazoo.readthedocs.org/en/latest/index.html.
Kazoo provides a higher-level child and data-watching APIs as a recipe through a module called kazoo.recipe.watchers
. This module provides the implementation of DataWatch
and ChildrenWatch
along with another class called PatientChildrenWatch
. The PatientChildrenWatch
class returns values after the children of a node don't change for a period of time, unlike the other two, which return each time an event is generated.
Let's look at the implementation of a simple children-watcher client, which will generate an event each time a znode is added or deleted from the ZooKeeper path:
import signal from kazoo.client import KazooClient from kazoo.recipe.watchers import ChildrenWatch zoo_path = '/MyPath' zk = KazooClient(hosts='localhost:2181') zk.start() zk.ensure_path(zoo_path) @zk.ChildrenWatch(zoo_path) def child_watch_func(children): print "List of Children %s" % children while True: signal.pause()
In this simple implementation of a children watcher, we connect to the ZooKeeper server that is running in the localhost, using the following code, and create a path /MyPath
:
zk.ensure_path(zoo_path) @zk.ChildrenWatch(zoo_path)
We then set a children watcher on this path and register a callback method child_watch_func, which prints the current list of children on the event generated in /MyPath
.
When we run this client watcher in a terminal, it starts listening to events:
On another terminal, we will create some znodes in/MyPath
with the ZooKeeper shell:
We observe that the children-watcher client receives these znode creation events, and it prints the list of the current children in the terminal window:
Similarly, if we delete the znodes that we just created, the watcher will receive the events and subsequently will print the children listing in the console:
The messages shown in the following screenshot are printed in the terminal where the children watcher is running:
3.133.126.239