English 中文(简体)
Can I recursively create a path in Zookeeper?
原标题:

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?

最佳回答

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.

问题回答

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.

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签