arsenal
arsenal

Reputation: 24154

How to watch on descendant child nodes in Python using kazoo?

I recently started working with Python for Zookeeper. I am using kazoo library for Zookeeper.

I have a very simple use case using kazoo for Zookeeper. I have a root node which is - /root. Now I need to keep a watch on the root node /root and if the new node which gets added to root node /root is /root/testing then I will only keep a watch on the /root/testing node. I don't want to keep a watch on any other node apart from the testing node. And then if any new children get added up to the /root/testing node, then I will keep a watch on them as well.

Let's say the child below that gets added up -

`/root/testing/test1`

Then I will keep a watch on test1 node as well.

Is this possible to do using Kazoo in Zookeeper? I am only able to keep a watch on one Zookeeper node (/root) with the code below:

#!/usr/bin/python
import time

from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()

########################################
@zk.ChildrenWatch("/root/testing")
def watch_children(children):
   print(children)

########################################

while True:
    time.sleep(5)

Can anyone help me with this in making multiple watches on the child node?

Upvotes: 4

Views: 3538

Answers (2)

user2961646
user2961646

Reputation:

Try something like this.

import time
from kazoo.client import KazooClient

zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()

children = zk.get_children("/root/",)
if "/testing" in children:
    children_testing = zk.get_children("/root/testing/")
        if children_testing != []: 
            @zk.ChildrenWatch("/root/testing")
            def watch_children(children):
                print(children)
        else:
            @zk.ChildrenWatch("/root/")
            def watch_children(children):
                print(children)
while True:
    time.sleep(5)

Upvotes: 2

trevorKirkby
trevorKirkby

Reputation: 1897

If you are trying to watch over multiple nodes, rather than one, you could use multiple threads (basically it runs different code simultaneously on different "threads"). Python has a threading module for doing simultaneous actions, which may be exactly what you want. Here is an example of code with threads implemented.

import threading

def watch_node(node):
    #implement your node watching script here

def watch_in_background(node):
    watcher = threading.Thread(target=watch_node,args=(node))   #initializes a thread that can run the watch_node function in the background, passing the node argument to it
    watcher.start()                                             #tells the thread to start running
    return watcher                                              #returns the thread object just in case you want to use it for something

watch_in_background(<node>)                                     #so this code will now watch the node in the background, allowing you to do other things, like watch multiple nodes

Upvotes: 0

Related Questions