【问题标题】:How to watch for events on the descendant nodes in ZooKeeper using kazoo?如何使用 kazoo 在 ZooKeeper 的后代节点上监视事件?
【发布时间】:2013-12-02 13:29:51
【问题描述】:

我最近开始为 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。如果workflow 节点被添加到/my/example 中,那么我将只关注/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 和@ 987654336@ 然后我需要打印/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教程来关注文档

【问题讨论】:

    标签: python apache-zookeeper watch kazoo


    【解决方案1】:

    试试这样的:

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 2012-08-19
      • 2013-12-03
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多