zktools.util

Utility functions for Zookeeper

zktools.util.safe_call(zk, func, *args, **kwargs)[source]

Safely call a function while handling connection loss

Note

This function merely retries the query until an active Zookeeper session is available that returns without throwing a ConnectionLoss Exception. When creating a node, this could result in a NodeAlreadyExists exception because the create ran twice.

zktools.util.safe_create_ephemeral_sequence(zk, name, data, acl)[source]

Safely creates an ephemeral sequence node using a UUID prefix

This function properly handles zookeeper ConnectionLoss exceptions and determines after waiting for reconnection whether it was created successfully.

Parameters:
  • zk – Zookeeper instance
  • name – Name of the node, it will be prefixed by the UUID
  • data – Data to set on the node
  • acl – ACL to set for the node
Returns:

Name of the created node

The name will be split so that its prefixed by the UUID and suffixed by the sequence.

Example:

>>> safe_create_ephemeral_sequence(zk, '/path/to/node/myname',
                                   [ZOO_OPEN_ACL_UNSAFE])
'/path/to/node/dfad3fa294d745e499d883b0a38bbc93-myname-0001'
zktools.util.threaded(func)[source]

Decorator to run a function in a separate thread

Parameters:func – Function to run in the other thread
Returns:Thread reference

Example:

@threaded
def task1():
    do_something

@threaded
def task2():
    do_something_too

t1 = task1()
t2 = task2()
...
t1.join()
t2.join()

Project Versions

This Page