You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Techy Teck <co...@gmail.com> on 2013/11/19 06:41:06 UTC

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

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)

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

Posted by Techy Teck <co...@gmail.com>.
Thanks a lot Diego. Let me see whether I can put that example using the
basic approach or not.

By decorations approach you mean the approach I am doing currently right
like this? -

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

Above is called decoration approach?




On Tue, Nov 19, 2013 at 12:00 PM, Diego Oliveira <lo...@gmail.com> wrote:

> Hello Techy,
>
>    The decorations approach you are doing may not be the best way to handle
> your use case. I don't know very much about the python binding but I think
> you'll need to do the watch programmatically to achieve the what you are
> looking for.  Take a look in the kazzo basic usage:
>
> def my_func(event):
>     # check to see what the children are now
> # Call my_func when the children changechildren =
> zk.get_children("/my/favorite/node", watch=my_func)
>
>
>
>
>
>
>
>
> On Tue, Nov 19, 2013 at 3:33 PM, Techy Teck <co...@gmail.com>
> wrote:
>
> > Can anyone help me with this? I am stuck on this problem..
> >
> > Or if there any better python client for Zookeeper then please let me
> know
> > as well?
> >
> >
> > On Mon, Nov 18, 2013 at 9:41 PM, Techy Teck <co...@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?
> > >
> > >
> > >     #!/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)
> > >
> >
>
>
>
> --
> Att.
> Diego de Oliveira
> System Architect
> diego@diegooliveira.com
> www.diegooliveira.com
> Never argue with a fool -- people might not be able to tell the difference
>

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

Posted by Diego Oliveira <lo...@gmail.com>.
Hello Techy,

   The decorations approach you are doing may not be the best way to handle
your use case. I don't know very much about the python binding but I think
you'll need to do the watch programmatically to achieve the what you are
looking for.  Take a look in the kazzo basic usage:

def my_func(event):
    # check to see what the children are now
# Call my_func when the children changechildren =
zk.get_children("/my/favorite/node", watch=my_func)








On Tue, Nov 19, 2013 at 3:33 PM, Techy Teck <co...@gmail.com> wrote:

> Can anyone help me with this? I am stuck on this problem..
>
> Or if there any better python client for Zookeeper then please let me know
> as well?
>
>
> On Mon, Nov 18, 2013 at 9:41 PM, Techy Teck <co...@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?
> >
> >
> >     #!/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)
> >
>



-- 
Att.
Diego de Oliveira
System Architect
diego@diegooliveira.com
www.diegooliveira.com
Never argue with a fool -- people might not be able to tell the difference

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

Posted by Techy Teck <co...@gmail.com>.
Can anyone help me with this? I am stuck on this problem..

Or if there any better python client for Zookeeper then please let me know
as well?


On Mon, Nov 18, 2013 at 9:41 PM, Techy Teck <co...@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?
>
>
>     #!/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)
>