当前位置: 首页 > 知识库问答 >
问题:

如何使用kazoo观看Python中的后代子节点?

柳韬
2023-03-14

我最近开始使用Python for ZooManager。我正在使用kazoo库for ZooManager。

我有一个非常简单的使用kazoo for Zookeeper的用例。我有一个根节点-/root。现在我需要监视根节点/root,如果添加到根节点/root的新节点是/root/testing,那么我只监视/root/testing节点。除了测试节点之外,我不想监视任何其他节点。然后,如果有任何新的子节点被添加到/root/testing节点,那么我也会监视它们。

假设下面的孩子加起来-

`/root/testing/test1`

然后我也会监视test1节点。

在动物园管理员中使用Kazoo可以做到这一点吗?我只能用下面的代码在一个动物园管理员节点(/root)上保持监视:

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

有人能帮我在子节点上制作多个手表吗?

共有2个答案

段坚
2023-03-14

如果您试图监视多个节点,而不是一个节点,您可以使用多个线程(基本上它在不同的“线程”上同时运行不同的代码)。Python有一个线程模块用于同时执行操作,这可能正是您想要的。这是一个实现线程的代码示例。

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
南宫阳焱
2023-03-14

试试这样。

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)
 类似资料:
  • 我最近开始为Zookeeper使用Python。我正在使用动物园管理员的库。我需要监视我的根节点- 可能会添加到我上面的根节点的其他几个节点如下- 现在我需要检查添加到根节点中的子节点是否为。如果节点被添加到中,那么我将只监视节点,如果任何新的子节点被添加到节点中,那么我也需要监视该节点。 比如说,的子代是,所以现在我需要监视,然后如果在这个节点上添加了任何新节点,比如和,那么我需要打印节点的子节

  • 我正在做一个项目,在这个项目中,我需要在一个节点上维护一个表,并且该节点也包括子节点。我尝试过使用PathCache,但我不知道如何在这里监视孩子们的孩子? 这里我的根节点是-,我正在使用下面的代码监视该节点。我想做的是,让手表保持在znode上。假设这些节点被添加到我的根节点- 然后我应该得到通知(直到这一部分我能够使它工作),但如果任何新节点被添加、更新或删除到,和,那么我也应该得到通知,而这

  • 问题内容: 我想获取所有属于以下子项的标签: 我知道如何找到像这样的特定类的元素: 但是我不知道如何找到所有的孩子,而不是其他孩子。 就像我想选择: 问题答案: 尝试这个

  • 问题内容: 我需要一些使用ng-bind-html创建的ng-model的帮助。我在服务器中有一个JSON文件,其中有一些html和一些类似的输入: .json 然后,在我的HTML文件中,我会看到以下内容: 在我的Angular控制器中,我有一个JSON文件的ajax调用: 控制器: 填充了html,但是我不能将$ watch用于通过这种方法生成的模型(q)。 我如何监视以此方式创建的模型的变化

  • Kazoo 是一个高等级的 Python 库,它能让 Apache Zookeeper 使用起来更加容易。

  • 问题内容: 我有一个文件,其中第一个字节包含编码信息。在Matlab中,我可以使用逐位读取字节,然后通过等读取每个位。 python中是否有任何等效的阅读器? 问题答案: 从文件中读取位,首先读取低位。