Question

I read the docs for kazoo. I then ran the code example on the site, the func for watch been called once every time I run it, I want to block the program until the children of one node deleted, how can I do this?

Current Code:

#!/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()
Was it helpful?

Solution

You can use threading.Lock to implement this requirement; it blocks the program until there is a child node of /distribute-lock delete or add.

Code:

#!/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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top