Zookeeper Counter





New in version 0.7: The Counter class.

Public API

class kazoo.recipe.counter.Counter(client, path, default=0, support_curator=False)[source]

Kazoo Counter

A shared counter of either int or float values. Changes to the counter are done atomically. The general retry policy is used to retry operations if concurrent changes are detected.

The data is marshaled using repr(value) and converted back using type(counter.default)(value) both using an ascii encoding. As such other data types might be used for the counter value.

If you would like to support clients updating the same znode path using either kazoo’s counter recipe or curator’s SharedCount recipe, you will need to enable the support_curator flag. This flag limits support to integers only and does not use ascii encoding as described above.

Counter changes can raise BadVersionError if the retry policy wasn’t able to apply a change.

Example usage:

zk = KazooClient()
counter = zk.Counter("/int")
counter += 2
counter -= 1
counter.value == 1
counter.pre_value == 2
counter.post_value == 1

counter = zk.Counter("/float", default=1.0)
counter += 2.0
counter.value == 3.0
counter.pre_value == 1.0
counter.post_value == 3.0

counter = zk.Counter("/curator", support_curator=True)
counter += 2
counter -= 1
counter.value == 1
counter.pre_value == 2
counter.post_value == 1
__init__(client, path, default=0, support_curator=False)[source]

Create a Kazoo Counter

  • client – A KazooClient instance.

  • path – The counter path to use.

  • default – The default value to use for new counter paths.

  • support_curator – Enable if support for curator’s SharedCount recipe is desired.


Add value to counter.


Subtract value from counter.