2013-04-19 4 views
1

나는 kazoo에 대한 문서를 읽었습니다. 그런 다음 사이트에서 코드 예제를 실행하고 watch를위한 func을 실행할 때마다 한 번 호출했습니다. 한 노드의 자식이 삭제 될 때까지 프로그램을 차단하고 싶습니다. 어떻게하면됩니까?kazoo에서 동물원 시계를 사용하여 프로그램을 차단하는 방법

현재 코드 :

#!/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() 

답변

2

당신은이 요구 사항을 구현하기 위해 threading.Lock을 사용할 수 있습니다;/distribute-lock 삭제 또는 추가의 자식 노드가있을 때까지 프로그램을 차단합니다.

코드 :

#!/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()