1

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()
StackExchange User
  • 1,222
  • 14
  • 35
xielingyun
  • 780
  • 9
  • 17

1 Answers1

2

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()
Viktor Haag
  • 3,363
  • 1
  • 18
  • 21
xielingyun
  • 780
  • 9
  • 17