Higher Level Zookeeper Client providing distributed coordination and configuration management primitives.
npx @tessl/cli install tessl/pypi-kazoo@2.10.0A comprehensive Python library that provides a higher-level API for Apache Zookeeper, designed to simplify distributed coordination and configuration management. Kazoo offers a rich set of distributed computing primitives including locks, leader elections, queues, barriers, and watchers, with built-in support for both synchronous and asynchronous programming models using threads, gevent, or eventlet.
pip install kazoofrom kazoo.client import KazooClientCommon patterns:
from kazoo.client import KazooClient
from kazoo.exceptions import KazooException, NoNodeError, NodeExistsError
from kazoo.security import make_digest_acl, OPEN_ACL_UNSAFE
from kazoo.recipe.lock import Lock
from kazoo.recipe.election import Election
from kazoo.recipe.queue import Queuefrom kazoo.client import KazooClient
import logging
logging.basicConfig()
# Create and start client
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
# Create a node
zk.create("/myapp", b"some data")
# Get data and stat
data, stat = zk.get("/myapp")
print(f"Version: {stat.version}, data: {data.decode('utf-8')}")
# Set data
zk.set("/myapp", b"updated data")
# List children
children = zk.get_children("/myapp")
# Clean up
zk.delete("/myapp")
zk.stop()Kazoo provides a layered architecture for Zookeeper interaction:
This design enables Kazoo to serve as both a low-level Zookeeper client and a high-level toolkit for building distributed systems with reliable coordination primitives.
Primary Zookeeper client functionality including connection management, CRUD operations, watches, transactions, and session handling. The KazooClient class provides the foundation for all Zookeeper interactions.
class KazooClient:
def __init__(self, hosts="127.0.0.1:2181", timeout=10.0, client_id=None,
handler=None, default_acl=None, auth_data=None, read_only=None,
randomize_hosts=True, connection_retry=None, command_retry=None,
logger=None, keyfile=None, certfile=None, ca=None, use_ssl=False,
verify_certs=True, **kwargs): ...
def start(self, timeout=15): ...
def stop(self): ...
def restart(self): ...
def close(self): ...
def create(self, path, value=b"", acl=None, ephemeral=False, sequence=False, makepath=False): ...
def get(self, path, watch=None): ...
def set(self, path, value, version=-1): ...
def delete(self, path, version=-1, recursive=False): ...
def exists(self, path, watch=None): ...
def get_children(self, path, watch=None, include_data=False): ...High-level distributed coordination primitives built on top of Zookeeper. These recipes provide common distributed systems patterns like locks, leader elections, queues, barriers, and counters with reliable semantics.
class Lock:
def __init__(self, client, path, identifier=None): ...
def acquire(self, blocking=True, timeout=None): ...
def release(self): ...
class Election:
def __init__(self, client, path, identifier=None): ...
def run(self, func, *args, **kwargs): ...
def cancel(self): ...
class Queue:
def __init__(self, client, path): ...
def put(self, value, priority=100): ...
def get(self, timeout=None): ...
class Barrier:
def __init__(self, client, path, num_clients): ...
def create(self): ...
def wait(self, timeout=None): ...Comprehensive exception hierarchy for handling all Zookeeper error conditions, including connection issues, node state conflicts, authentication failures, and protocol errors. Each exception includes appropriate error codes and context.
class KazooException(Exception): ...
class ZookeeperError(KazooException): ...
class ConnectionLoss(ZookeeperError): ...
class NoNodeError(ZookeeperError): ...
class NodeExistsError(ZookeeperError): ...
class SessionExpiredError(ZookeeperError): ...
class AuthFailedError(ZookeeperError): ...Access control and authentication mechanisms for securing Zookeeper nodes. Includes ACL creation, permission management, and authentication schemes with support for digest, IP, and world authentication.
class ACL:
def __init__(self, perms, id): ...
class Permissions:
READ: int
WRITE: int
CREATE: int
DELETE: int
ADMIN: int
ALL: int
def make_digest_acl(username, password, read=False, write=False, create=False, delete=False, admin=False, all=False): ...
def make_acl(scheme, credential, perms): ...Pluggable concurrency models supporting different async frameworks. Handlers manage callback execution, timeouts, and async result objects with support for threading, gevent, and eventlet paradigms.
class SequentialThreadingHandler:
def __init__(self): ...
def start(self): ...
def stop(self): ...
class SequentialGeventHandler:
def __init__(self): ...
class SequentialEventletHandler:
def __init__(self): ...Testing utilities and harnesses for developing Zookeeper-based applications. Includes managed Zookeeper server instances, test clusters, and base test classes with automatic client setup and teardown.
class KazooTestHarness:
def setup_zookeeper(self): ...
def teardown_zookeeper(self): ...
class KazooTestCase:
def setUp(self): ...
def tearDown(self): ...from collections import namedtuple
from typing import Optional, List, Tuple, Callable, Any, Union
# Core types
ZnodeStat = namedtuple('ZnodeStat', 'czxid mzxid ctime mtime version cversion aversion ephemeralOwner dataLength numChildren pzxid')
WatchedEvent = namedtuple('WatchedEvent', 'type state path')
ACL = namedtuple('ACL', 'perms id')
Id = namedtuple('Id', 'scheme id')
# State enums
class KazooState:
SUSPENDED: str
CONNECTED: str
LOST: str
class KeeperState:
EXPIRED_SESSION: int
AUTH_FAILED: int
CLOSED: int
CONNECTED: int
CONNECTING: int
class EventType:
CREATED: int
DELETED: int
CHANGED: int
CHILD: int