我最近开始为Zookeeper使用Python。我正在为Zookeeper使用kazoo库。我需要监视我的根节点-
/my/example可能添加到我上面的根节点的其他几个节点将如下所示-
/my/example/workflow
/my/example/workflow/v1
/my/example/workflow/v1/step1
/my/example/workflow/v1/step2现在我需要检查在根节点/my/example中添加的子节点是否为/my/example/workflow。如果在/my/example中添加了workflow节点,那么我将只关注/my/example/workflow节点,如果在/my/example/workflow节点中添加了任何新的子节点,那么我也需要关注该节点。
假设/my/example/workflow的子节点是/my/example/workflow/v1,所以现在我需要监视/my/example/workflow/v1,然后如果在这个节点/my/example/workflow/v1上添加了任何新节点,比如/my/example/workflow/v1/step1和/my/example/workflow/v1/step2,那么我需要打印/my/example/workflow/v1节点的子节点,并且现在不会生成任何新的监视。
现在我不确定如何继续调用我的孩子的手表,直到某个点,在这个例子中,直到/my/example/workflow/v1,我需要继续观察,一旦所有的步骤节点相加,我需要打印/my/example/workflow/v1的孩子。下面是我的代码,它只在一个根节点上运行良好,现在我不确定如何解决上面的问题?
#!/usr/bin/python
from kazoo.client import KazooClient
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
@zk.ChildrenWatch("/my/example")
def watch_children(children):
print("Children are now: %s" % children)在这方面,任何帮助都是非常感谢的。我通过阅读来自here的kazoo教程来遵循文档。
发布于 2013-11-26 05:58:52
试试下面这样的东西:
import time
from kazoo.client import KazooClient
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
children = zk.get_children("/my/example/")
if "/workflow" in children:
children_testing = zk.get_children("/my/example/testing/")
if children_testing != []:
@zk.ChildrenWatch("/my/example/testing")
def watch_children(children):
print(children)
else:
@zk.ChildrenWatch("/my/example/")
def watch_children(children):
print(children)
while True:
time.sleep(5)https://stackoverflow.com/questions/20064036
复制相似问题