zktools.node

Zookeeper Nodes

This module provides a ZkNode object which can represent a single node from Zookeeper. It can reflect a single value, or a JSON serialized value.

Node Class

class zktools.node.ZkNode(connection, path, default=None, use_json=False, permission={'perms': <class 'Mock'>, 'scheme': 'world', 'id': 'anyone'}, create_mode=0)[source]

Zookeeper Node

This object provides access to a single node for updating the value that can also track changes to the value from Zookeeper. Functions can be subscribed to changes in the node’s value and/or changes in the node’s children (nodes under it being created/removed).

The value of the node is coerced into an appropriate Python object when loaded, current supported conversions:

Numbers with decimals      -> Decimal
Numbers without decimals   -> Int
true/false                 -> Bool
none                       -> None
ISO 8601 Date and Datetime -> date or datetime

And optionally, with use_json:

JSON string                -> dict/list

Note

The JSON determination is extremely lax, if it is a string that starts and ends with brackets or curly marks, it is assumed to be a JSON object and will be coerced if possible. If coercion fails, the string will be returned as is.

Example:

from zc.zk import ZooKeeper
from zktools.node import ZkNode

conn = ZooKeeper()
node = ZkNode(conn, '/some/config/node')

# prints out the current value, defaults to None
print node.value

# Set the value in zookeeper
node.value = 483.24

# Show the children of the node
print list(node.children)

# Subscribe a function to be called when the node's
# children change (note this will be called immediately
# with a zc.zk Children instance)
@node.children
def my_function(children):
    # do something with node.children or prior_children

The default behavior is to track changes to the node, so that the value attribute always reflects the node’s value in Zookeeper. Additional subscriber functions are called when the Zookeeper event watch is triggered and are run in a separate event thread. Depending on how fast the value/children are changing the subscriber functions may run consecutively and could miss intermediate values.

Return values of subscriber functions are ignored.

Warning

Do not delete nodes that are in use, there intentionally is no code to handle such conditions as it creates overly complex scenarios both for ZkNode and for application code using it.

__init__(connection, path, default=None, use_json=False, permission={'perms': <class 'Mock'>, 'scheme': 'world', 'id': 'anyone'}, create_mode=0)[source]

Create a Zookeeper Node

Creating a ZkNode by default attempts to load the value, and if it is not found will automatically create a blank string as the value.

The last time a ZkNode has been modified either by the user or due to a Zookeeper update is recorded as the ZkNode.last_modified attribute, as a long in

milliseconds from epoch.
Parameters:
  • connection (zc.zk Zookeeper instance) – Zookeeper connection object
  • path (str) – Path to the Zookeeper node
  • default – A default value if the node is being created
  • use_json (bool) – Whether values that look like a JSON object should be deserialized, and dicts/lists saved as JSON.
  • permission (dict) – Node permission to use if the node is being created.
  • create_mode (int) – Persistent or ephemeral creation mode
value[source]

Returns the current value

If the Zookeeper session expired, it will be reconnected and the value reloaded.

connected[source]

Indicate whether a connection to Zookeeper exists

Project Versions

Table Of Contents

Previous topic

zktools.locking

Next topic

Changelog

This Page