or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

core-client.mdexceptions.mdhandlers.mdindex.mdrecipes.mdsecurity.mdtesting.md
tile.json

tessl/pypi-kazoo

Higher Level Zookeeper Client providing distributed coordination and configuration management primitives.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/kazoo@2.10.x

To install, run

npx @tessl/cli install tessl/pypi-kazoo@2.10.0

index.mddocs/

Kazoo

A 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.

Package Information

  • Package Name: kazoo
  • Language: Python
  • Installation: pip install kazoo
  • Version: 2.10.0
  • Requirements: Python 3.8+

Core Imports

from kazoo.client import KazooClient

Common 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 Queue

Basic Usage

from 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()

Architecture

Kazoo provides a layered architecture for Zookeeper interaction:

  • KazooClient: Main client class handling connection, session management, and basic operations
  • Protocol Layer: Low-level Zookeeper protocol implementation with serialization and connection handling
  • Recipe Layer: High-level distributed algorithms (locks, barriers, queues, elections)
  • Handler Layer: Pluggable async implementations (threading, gevent, eventlet)
  • Security Layer: ACL management and authentication mechanisms

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.

Capabilities

Core Client Operations

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): ...

Core Client

Distributed Recipes

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): ...

Distributed Recipes

Exception Handling

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): ...

Exception Handling

Security and ACL Management

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): ...

Security and ACLs

Async Handlers

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): ...

Async Handlers

Testing Infrastructure

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): ...

Testing Infrastructure

Types

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