Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用kazoo中的zookeeper手表阻止节目_Python 3.x_Apache Zookeeper_Watch_Kazoo - Fatal编程技术网

Python 3.x 如何使用kazoo中的zookeeper手表阻止节目

Python 3.x 如何使用kazoo中的zookeeper手表阻止节目,python-3.x,apache-zookeeper,watch,kazoo,Python 3.x,Apache Zookeeper,Watch,Kazoo,我读了一段时间的文件。 然后我在网站上运行了代码示例,每次运行func for 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_c

我读了一段时间的文件。 然后我在网站上运行了代码示例,每次运行func for 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()

您可以使用
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()