2-zakoo使用
source:http://kazoo.readthedocs.io/en/latest/basic_usage.html
1 基本使用
1.1 连接处理
要使用zakoo,需要创建一个KazooClient对象,来建立一个和服务器的连接。
zakoo有一个状态系统,可以注册一个listerner,在状态改变的时候被调用。有三种状态:lost,connected,suspended。
1.2 CRUD
zakoo可以在znode上进行create、read、update、delete操作。
create:
ensure_path(),可以在多层目录中创建不存在的中层目录。但不可以设定节点的内容。
create(),创建节点和内容。
read:
exists(),查看一个节点是否存在
get(),抓取节点内容。
get_children(),获取子节点列表
update:
set(),操作和create一致。
delete:
delete(),删除节点,可以递归删除。
1.3 watchers
设置检测器,在节点改变或子节点改变时触发。
第一种设定方式:只接受一次调用,不接受会话事件。在get和get_children里面设定。
第二种设定方式:每次改变都会触发,不需要重设检测器。
ChildrenWatch
DataWatch
1.4 transactions
将多个命令作为一个单元提交。
好像没什么用,再说。。
1.5 测试代码
import time
from kazoo.client import KazooClient
from kazoo.client import KazooState def main():
#-----------建立连接-----------
zk = KazooClient(hosts='192.168.10.12:2181')
zk.start() @zk.add_listener
def my_listener(state):
if state == KazooState.LOST:
print('conn lost')
elif state == KazooState.SUSPENDED:
print('conn sus')
else:
print('conn ok') #---------写入节点信息----------
#zk.create("/worker/process1/125",b"i am award process.") #--------读取节点信息----------
#if zk.exists("/worker/process1/125"):
# print("125 exists.") #data,stat = zk.get('/worker/process1/125')
#print(data) #children = zk.get_children('/worker/process1')#
#print(children) #-------改写节点信息---------
#zk.set("/worker/process1/125",b"i am tally process.")
#data,stat = zk.get('/worker/process1/125')
#print(data) #--------删除节点-----------
#zk.delete('/worker/process1',recursive=True)
#pchildren = zk.get_children('/worker')
#rint(children) #--------设置检测器---------
@zk.ChildrenWatch('/worker')
def watch_children(children):
print("Children are now: %s" % children) @zk.DataWatch('/worker/process2')
def watch_node(data,stat):
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8"))) time.sleep(2)
zk.stop() if __name__ == '__main__':
main()
2-zakoo使用的更多相关文章
随机推荐
- C++再论单例模式
#include <iostream> #include <windows.h> #include <mutex> std::mutex gmutex; using ...
- Arduino 网络时钟client
升级! 添加了12h/24h 的开关,还有标准/ 夏令时开关!见步骤7 & 步骤8. 你是否曾想要一个和办公室时间来源全然准确的表? 这就有一个网络的办公时间server,你能够根据它并同步你 ...
- Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn
Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...
- 安装sbt
http://www.scala-sbt.org/0.13/docs/zh-cn/Installing-sbt-on-Linux.html [root@hadoop1 target]# curl ht ...
- kbmMemTable关于内存表的使用,以及各种三层框架的评价
关于内存表的使用(kbmMemTable) 关于内存表的使用说明一. Delphi使用内存表1.1 Delphi创建内存表步骤:1. 创建一个Ttable实例.2. 设置一个DataBaseName为 ...
- POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ3685 Matrix —— 二分
题目链接:http://poj.org/problem?id=3685 Matrix Time Limit: 6000MS Memory Limit: 65536K Total Submissio ...
- 软件GUI测试中的关注点
[摘要] 本文列数了软件黑盒测试过程中,在被测试软件中可能存在的常见软件问题.本文不会详细讨论基本的软件测试思想与常用技术,仅针对在软件黑盒测试过程中若干的问题做描述,并提供个人的参考测试意见与防范意 ...
- java邮件发送(含附件)
1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在sr ...
- html5--3.2 input元素(2)
html5--3.2 input元素(2) 学习要点 input元素及其属性 input元素 用来设置表单中的内容项,比如输入内容的文本框,按钮等 不仅可以布置在表单中,也可以在表单之外的元素使用 i ...