首页 文章

如何使用kazoo监视ZooKeeper中后代节点上的事件?

提问于
浏览
1

我最近开始使用Python for Zookeeper . 我正在为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 节点的子节点而我赢了现在不做任何新 Watch .

现在我不知道怎么继续给我的孩子们打电话直到某一点,在这种情况下直到 /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教程来关注文档

1 回答

  • 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)
    

相关问题