How to watch for events on the descendant nodes in ZooKeeper using kazoo?

216 views
Skip to first unread message

compte...@gmail.com

unread,
Nov 19, 2013, 12:41:37 AM11/19/13
to pyth...@googlegroups.com
I recently started working with Python for Zookeeper. I am using `kazoo` library for Zookeeper. I need to keep a watch on my root node which is -

    /my/example
   
couple of other nodes which might get added to my above root node will be like this -

    /my/example/workflow
    /my/example/workflow/v1
    /my/example/workflow/v1/step1
    /my/example/workflow/v1/step2


Now I need to check whether the children which got added in root node `/my/example` is `/my/example/workflow` or not. If `workflow` node gets added up in `/my/example` then I will keep a watch on `/my/example/workflow` node only and if any new children gets added up in `/my/example/workflow` node then I need to keep a watch on that node as well.

Let's say, children of `/my/example/workflow` is `/my/example/workflow/v1`, so now I need to keep a watch on `/my/example/workflow/v1` and then if any new nodes gets added up on this node `/my/example/workflow/v1` such as `/my/example/workflow/v1/step1` and `/my/example/workflow/v1/step2` then I need to print the children of `/my/example/workflow/v1` node and I won't make any new watches now.

Now I am not sure how to keep on calling watches on my children until certain point, here in this case till `/my/example/workflow/v1` I need to keep on watching and as soon as all the steps nodes gets added up, I need to print the children of `/my/example/workflow/v1`. Below is my code which works fine for watching on only one root node and now I am not sure how to do my above problem?


    #!/usr/bin/python
    from kazoo.client import KazooClient

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

    @zk.ChildrenWatch("/my/example")
    def watch_children(children):
        print("Children are now: %s" % children)

Any help is really appreciated on this. I was following the documentation by reading the kazoo tutorial from [here](http://kazoo.readthedocs.org/en/latest/basic_usage.html#watchers)

Jyrki Pulliainen

unread,
Nov 20, 2013, 3:59:40 AM11/20/13
to compte...@gmail.com, pyth...@googlegroups.com
On Tue, Nov 19, 2013 at 6:41 AM, <compte...@gmail.com> wrote:
I recently started working with Python for Zookeeper. I am using `kazoo` library for Zookeeper. I need to keep a watch on my root node which is -

    /my/example
   
couple of other nodes which might get added to my above root node will be like this -

    /my/example/workflow
    /my/example/workflow/v1
    /my/example/workflow/v1/step1
    /my/example/workflow/v1/step2
 

Now I need to check whether the children which got added in root node `/my/example` is `/my/example/workflow` or not. If `workflow` node gets added up in `/my/example` then I will keep a watch on `/my/example/workflow` node only and if any new children gets added up in `/my/example/workflow` node then I need to keep a watch on that node as well.

Let's say, children of `/my/example/workflow` is `/my/example/workflow/v1`, so now I need to keep a watch on `/my/example/workflow/v1` and then if any new nodes gets added up on this node `/my/example/workflow/v1` such as `/my/example/workflow/v1/step1` and `/my/example/workflow/v1/step2` then I need to print the children of `/my/example/workflow/v1` node and I won't make any new watches now.

Now I am not sure how to keep on calling watches on my children until certain point, here in this case till `/my/example/workflow/v1` I need to keep on watching and as soon as all the steps nodes gets added up, I need to print the children of `/my/example/workflow/v1`. Below is my code which works fine for watching on only one root node and now I am not sure how to do my above problem?


I'm guessing the name of the "workflow" node is not known beforehand? To catch these events, you need to register a ChildrenWatcher eon the /my/example, as you have done in your code below. When an event happens and a node gets added (you need to keep track of children for this), you just add a new ChildrenWatcher for that etc.
 
ZooKeeper (and thus Kazoo) does not provide notifications on deeper subtree changes, so you need to work around it by assigning new watchers manually when the tree grows

- Jyrki


    #!/usr/bin/python
    from kazoo.client import KazooClient

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

    @zk.ChildrenWatch("/my/example")
    def watch_children(children):
        print("Children are now: %s" % children)

Any help is really appreciated on this. I was following the documentation by reading the kazoo tutorial from [here](http://kazoo.readthedocs.org/en/latest/basic_usage.html#watchers)

--
You received this message because you are subscribed to the Google Groups "python-zk" group.
Visit this group at http://groups.google.com/group/python-zk.



--
- Jyrki

compte...@gmail.com

unread,
Nov 20, 2013, 1:25:30 PM11/20/13
to pyth...@googlegroups.com, compte...@gmail.com
Thanks a lot Jyrki for the suggestion. I recently started working with Python and I don't have any background with Python at all so that is the reason I am facing lot of problem.

And Yes, I am aware of that I might need to place a watcher on each children but I am stuck on that.

Is there any way if you can provide me a simple example for this by which I will be able to understand better and I can take that froward from that point onwards. I am stuck on this stage only so I am not able to code further.

Any help will be really appreciated on this. My bad, I don't have any experience with Python which is causing me a problem..
Reply all
Reply to author
Forward
0 new messages