我读了kazoo的文档。然后我在网站上运行了代码示例,watch的函数每次运行时都会调用一次,我想阻塞程序,直到一个节点的子节点被删除,我该怎么做呢?
当前代码:
#!/usr/bin/env python3
from kazoo.client import KazooClient
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
@zk.ChildrenWatch("/distribute-lock")
def watch_children(children):
print("Children are now: %s" % children)
children = zk.exists("/distribute-lock/childnode-325", watch=watch_children)
print(children)
zk.stop()发布于 2013-04-22 10:28:58
您可以使用threading.Lock来实现这个需求;它会阻塞程序,直到有/distribute lock、delete或add的子节点。
代码:
#!/usr/bin/env python3
from kazoo.client import KazooClient
from threading import Lock
zkl = Lock()
def my_func(event):
if event.type == 'CHILD':
zkl.release()
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()
zkl.acquire()
children = zk.get_children("/distribute-lock", watch=my_func)
zkl.acquire()
zk.stop()https://stackoverflow.com/questions/16096489
复制相似问题