Skip to content

Instantly share code, notes, and snippets.

@dgabriele
Last active December 19, 2015 05:39
Show Gist options
  • Save dgabriele/5905588 to your computer and use it in GitHub Desktop.
Save dgabriele/5905588 to your computer and use it in GitHub Desktop.
This is an example of working with Kazoo.fs.FileSystem. The effect of this code is to atomically fetch and increment a counter associated with a hypothetical "user" znode, called "frank".
''' file: kazoo-fs-example.py
- See: https://github.com/basefook/kazoo/blob/master/kazoo/fs.py
WHAT IS THIS?
----------------------
This is an example of working with FileSystem. The effect of this
code is to atomically fetch and increment a counter associated with
a hypothetical "user" znode, called "frank".
THINGS TO NOTICE
----------------------
- Sports a familiar API. For instance: ls, cd, touch, rm, etc.
- If no client is passed to the FileSystem constructor, a default
client is instantiated and started automatically. If this is the
case, then the FileSystem destructor automatically stops
the client session.
- The first with-statement is called on the instance itself. This
has the effect of save the present working path or "PWD", which is
restored upon exiting the block.
- The semantics of the "cd" method are self-evident. However, both
this and all other FileSystem methods can take EITHER absolute or
relative paths. For instance, .., foo/bar, ../../foo, /spam are all
valid paths. Paths can also be specified as component sequences,
like ('foo', 'bar').
- In regards to both "lock" and "counter" methods, these return a
new or cached instance of a Lock or Counter recipe object,
respectively. Notice that each is passed a relative path argument.
'''
from kazoo.fs import FileSystem
zkfs = FileSystem(
# None
# path = '/'
)
try:
with zkfs:
zkfs.cd('users/frank')
with zkfs.lock('likes/lock'):
counter = zkfs.counter('likes/int', default=1)
count = counter.value
counter += 1
except Exception as e:
exit(str(e))
print "Frank now has {0} likes!".format(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment