{"id":1524,"library":"kazoo","title":"Kazoo","description":"Kazoo is a higher-level Python client for Apache ZooKeeper, providing robust abstractions for common distributed coordination tasks like locks, leader election, and queues. Version 2.11.0 is the latest stable release, with development active and releases occurring periodically, often driven by Python version support updates and bug fixes.","status":"active","version":"2.11.0","language":"en","source_language":"en","source_url":"https://github.com/python-zk/kazoo","tags":["zookeeper","distributed systems","coordination","async"],"install":[{"cmd":"pip install kazoo","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"KazooClient","correct":"from kazoo.client import KazooClient"},{"symbol":"KazooException","correct":"from kazoo.exceptions import KazooException"}],"quickstart":{"code":"import os\nimport time\nfrom kazoo.client import KazooClient\nfrom kazoo.exceptions import KazooException\n\n# Get Zookeeper host(s) from environment variable, e.g., '127.0.0.1:2181,127.0.0.1:2182'\nZOOKEEPER_HOSTS = os.environ.get('ZOOKEEPER_HOSTS', '127.0.0.1:2181')\n\nzk = KazooClient(hosts=ZOOKEEPER_HOSTS)\n\n@zk.add_listener\ndef my_listener(state):\n    \"\"\"Listener for Zookeeper connection state changes.\"\"\"\n    print(f\"Zookeeper state changed: {state}\")\n    if state == 'CONNECTED':\n        print(\"Successfully connected to Zookeeper!\")\n    elif state == 'LOST':\n        print(\"Connection to Zookeeper lost. Attempting to reconnect...\")\n    elif state == 'SUSPENDED':\n        print(\"Connection to Zookeeper suspended. Will attempt to reconnect.\")\n\ntry:\n    print(f\"Attempting to connect to Zookeeper at {ZOOKEEPER_HOSTS}...\")\n    zk.start()\n    zk.ensure_path(\"/my/kazoo/path\")\n    print(\"Created /my/kazoo/path if it didn't exist.\")\n\n    # Create an ephemeral node that will be deleted when the client disconnects\n    node_path = \"/my/kazoo/path/ephemeral_node\"\n    zk.create(node_path, b\"hello_kazoo\", ephemeral=True, sequence=False)\n    print(f\"Created ephemeral node: {node_path} with data 'hello_kazoo'\")\n\n    data, stat = zk.get(node_path)\n    print(f\"Retrieved data from {node_path}: {data.decode('utf-8')}, Stat: {stat}\")\n\n    # Keep the client alive for a few seconds to observe state changes or for other operations\n    time.sleep(5)\n\nexcept KazooException as e:\n    print(f\"A Kazoo-specific error occurred: {e}\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")\nfinally:\n    if zk.connected:\n        zk.stop()\n        print(\"KazooClient stopped.\")\n    zk.close()\n    print(\"KazooClient closed.\")","lang":"python","description":"This quickstart demonstrates how to connect to Apache ZooKeeper using Kazoo, set up a connection state listener, create an ephemeral node, and retrieve its data. Remember to have a ZooKeeper instance running and set the `ZOOKEEPER_HOSTS` environment variable accordingly."},"warnings":[{"fix":"Refer to the latest Kazoo release notes or documentation for current Python compatibility. For example, Kazoo 2.10.0 dropped support for Python 3.7, now supporting Python 3.8-3.12.","message":"Kazoo frequently updates its supported Python versions, often dropping compatibility with older Python releases. Ensure your environment matches the supported versions to avoid compatibility issues.","severity":"breaking","affected_versions":"2.8.0+"},{"fix":"Implement a connection state listener using `@zk.add_listener` and structure your logic to react to connection events. Ensure `zk.start()` is called and the client has connected before attempting Zookeeper operations.","message":"KazooClient operates asynchronously. It's crucial to correctly manage the client's lifecycle (start, stop, close) and handle connection state changes (CONNECTED, SUSPENDED, LOST) using listeners or by checking `zk.connected` before performing operations. Operations performed while not connected can lead to errors or unexpected behavior.","severity":"gotcha","affected_versions":"all versions"},{"fix":"Always provide the Zookeeper host string in the correct `host:port,host:port` format. Validate the string if it's sourced from configuration or environment variables.","message":"The `hosts` parameter for `KazooClient` expects a comma-separated string of `host:port` pairs (e.g., '127.0.0.1:2181,127.0.0.2:2181'). Using spaces or other delimiters can lead to connection failures.","severity":"gotcha","affected_versions":"all versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}