Take the 2-minute tour ×

I'm pulling ZooKeeper into a project for some concurrency management, and the first thing I tried was something that, to me, was quite obvious (using the zkpython binding):

zh = zookeeper.init('localhost:2181')
zookeeper.create(zh, '/path/to/a/node', '', [ZOO_OPEN_ACL_UNSAFE])

And I got back a NoNodeException for my trouble.

After reflecting on this and reviewing the docs (such as they are), I've been unable to find a way to do the equivalent of a mkdir -p where ZooKeeper will create the missing parent nodes for me.

Am I missing anything, or am I just stuck issuing separate create()s for each part of a path whether I like it or not?

share|improve this question
Where can I find ZOO_OPEN_ACL_UNSAFE? –  xitrium Mar 23 '11 at 22:16
add comment

3 Answers

up vote 5 down vote accepted

You're stuck to issue separate create()s for each element of the path. Zookeeper has only atomic operations build in. Creating a path recursively is not an atomic operation anymore. Zookeeper could not know, what you want it to do, if the operation hangs after creating half of the path elements. I don't know, if there's already a Zookeeper helper library in python. There is one in java (zkClient) that will let you create recursive paths by calling create() multiple times.

share|improve this answer
So far the only Python interface for Zookeeper I've found is the one in the Zookeeper tree (which is just basic bindings to the core API), so I guess I'll be writing my own convenience methods. :) Thanks! –  Nicholas Knight Jul 27 '10 at 16:28
add comment

If you issue separate create()s, you might get interrupted or fail when you're halfway through. To make the call atomic, you can use the new multi() API. See this answer.

If the path or a part of it might already exist, then waiting for each create() to finish before issuing the next would be unnecessarily slow. In this case, you can use the asynchronous API to speed up the process. See this answer.

If you just want to avoid the extra calls, you can use Netflix's curator library which has a creatingParentsIfNeeded method, but be advised that it might be slow. See this answer.

share|improve this answer
add comment

Kazoo has an ensure_path(path) operation, although it isn't considered atomic. Using this would at least save you the need to write your own code for a recursive create.

share|improve this answer
add comment

Your Answer


