Zookeeper接口kazoo实例解析


Posted in Python onJanuary 22, 2018

本文主要研究的是Zookeeper接口kazoo的相关内容,具体介绍如下。

zookeeper的开发接口以前主要以java和c为主,随着python项目越来越多的使用zookeeper作为分布式集群实现,python的zookeeper接口也出现了很多,现在主流的纯python的zookeeper接口是kazoo。因此如何使用kazoo开发基于python的分布式程序是必须掌握的。

1.安装kazoo

yum install python-pip
pip install kazoo

安装过程中会出现一些python依赖包未安装的情况,安装即可。

2.运行kazoo基础例子kazoo_basic.py

import time
from kazoo.client import KazooClient
from kazoo.client import KazooState
def main():
 zk=KazooClient(hosts='127.0.0.1:2182')
 zk.start()
 
 @zk.add_listener
 def my_listener(state):
 if state == KazooState.LOST:
  print("LOST")
 elif state == KazooState.SUSPENDED:
  print("SUSPENDED")
 else:
  print("Connected")
 #Creating Nodes
 # Ensure a path, create if necessary
 zk.ensure_path("/my/favorite")
 # Create a node with data
 zk.create("/my/favorite/node", b"")
 zk.create("/my/favorite/node/a", b"A")
 #Reading Data
 # Determine if a node exists
 if zk.exists("/my/favorite"):
 print("/my/favorite is existed")
 @zk.ChildrenWatch("/my/favorite/node")
 def watch_children(children):
 print("Children are now: %s" % children)
 # Above function called immediately, and from then on
 @zk.DataWatch("/my/favorite/node")
 def watch_node(data, stat):
 print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
 # Print the version of a node and its data
 data, stat = zk.get("/my/favorite/node")
 print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
 # List the children
 children = zk.get_children("/my/favorite/node")
 print("There are %s children with names %s" % (len(children), children))
 #Updating Data
 zk.set("/my/favorite", b"some data")
 #Deleting Nodes
 zk.delete("/my/favorite/node/a")
 #Transactions
 transaction = zk.transaction()
 transaction.check('/my/favorite/node', version=-1)
 transaction.create('/my/favorite/node/b', b"B")
 results = transaction.commit()
 print ("Transaction results is %s" % results)
 zk.delete("/my/favorite/node/b")
 zk.delete("/my", recursive=True)
 time.sleep(2)
 zk.stop()
if __name__ == "__main__":
 try:
 main()
 except Exception, ex:
 print "Ocurred Exception: %s" % str(ex)
 quit()

运行结果:

Children are now: [u'a']
Version: 0, data: 
Version: 0, data: 
There are 1 children with names [u'a']
Children are now: []
Transaction results is [True, u'/my/favorite/node/b']
Children are now: [u'b']
Children are now: []
No handlers could be found for logger "kazoo.recipe.watchers"
LOST

以上程序运行了基本kazoo接口命令,包括创建删除加watcher等操作,通过调试并对比zookeeper服务节点znode目录结构的变化,就可以理解具体的操作结果。

3.运行通过kazoo实现的分布式锁程序kazoo_lock.py

import logging, os, time
from kazoo.client import KazooClient
from kazoo.client import KazooState
from kazoo.recipe.lock import Lock

class ZooKeeperLock():
 def __init__(self, hosts, id_str, lock_name, logger=None, timeout=1):
 self.hosts = hosts
 self.id_str = id_str
 self.zk_client = None
 self.timeout = timeout
 self.logger = logger
 self.name = lock_name
 self.lock_handle = None
 self.create_lock()
 def create_lock(self):
 try:
 self.zk_client = KazooClient(hosts=self.hosts, logger=self.logger, timeout=self.timeout)
 self.zk_client.start(timeout=self.timeout)
 except Exception, ex:
 self.init_ret = False
 self.err_str = "Create KazooClient failed! Exception: %s" % str(ex)
 logging.error(self.err_str)
 return
 try:
 lock_path = os.path.join("/", "locks", self.name)
 self.lock_handle = Lock(self.zk_client, lock_path)
 except Exception, ex:
 self.init_ret = False
 self.err_str = "Create lock failed! Exception: %s" % str(ex)
 logging.error(self.err_str)
 return
 def destroy_lock(self):
 #self.release()
 if self.zk_client != None:
 self.zk_client.stop()
 self.zk_client = None
 def acquire(self, blocking=True, timeout=None):
 if self.lock_handle == None:
 return None
 try:
 return self.lock_handle.acquire(blocking=blocking, timeout=timeout)
 except Exception, ex:
 self.err_str = "Acquire lock failed! Exception: %s" % str(ex)
 logging.error(self.err_str)
 return None
 def release(self):
 if self.lock_handle == None:
 return None
 return self.lock_handle.release()
 def __del__(self):
 self.destroy_lock()

def main():
 logger = logging.getLogger()
 logger.setLevel(logging.INFO)
 sh = logging.StreamHandler()
 formatter = logging.Formatter('%(asctime)s -%(module)s:%(filename)s-L%(lineno)d-%(levelname)s: %(message)s')
 sh.setFormatter(formatter)
 logger.addHandler(sh)
 zookeeper_hosts = "127.0.0.1:2182"
 lock_name = "test"
 lock = ZooKeeperLock(zookeeper_hosts, "myid is 1", lock_name, logger=logger)
 ret = lock.acquire()
 if not ret:
 logging.info("Can't get lock! Ret: %s", ret)
 return
 logging.info("Get lock! Do something! Sleep 10 secs!")
 for i in range(1, 11):
 time.sleep(1)
 print str(i)
 lock.release()

if __name__ == "__main__":
 try:
 main()
 except Exception, ex:
 print "Ocurred Exception: %s" % str(ex)
 quit()

将该测试文件copy到多个服务器,同时运行,就可以看到分布式锁的效果了。

总结

以上就是本文关于Zookeeper接口kazoo实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
python将人民币转换大写的脚本代码
Feb 10 Python
Python中元组,列表,字典的区别
May 21 Python
Python用sndhdr模块识别音频格式详解
Jan 11 Python
详解python中的装饰器
Jul 10 Python
Python图像处理库PIL的ImageFont模块使用介绍
Feb 26 Python
Python+Appium实现自动化测试的使用步骤
Mar 24 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
在ipython notebook中使用argparse方式
Apr 20 Python
Python CategoricalDtype自定义排序实现原理解析
Sep 11 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
Python 调用 ES、Solr、Phoenix的示例代码
Nov 23 Python
Python turtle编写简单的球类小游戏
Mar 31 Python
Python调用C语言的方法【基于ctypes模块】
Jan 22 #Python
python的Crypto模块实现AES加密实例代码
Jan 22 #Python
python实现求最长回文子串长度
Jan 22 #Python
Python获取本机所有网卡ip,掩码和广播地址实例代码
Jan 22 #Python
Linux CentOS7下安装python3 的方法
Jan 21 #Python
简述Python2与Python3的不同点
Jan 21 #Python
手把手教你用python抢票回家过年(代码简单)
Jan 21 #Python
You might like
PHP分页显示制作详细讲解
2006/12/05 PHP
php URL验证正则表达式
2011/07/19 PHP
php操作csv文件代码实例汇总
2014/09/22 PHP
PHP实现操作redis的封装类完整实例
2015/11/14 PHP
PHP操作redis实现的分页列表,新增,删除功能封装类与用法示例
2018/08/04 PHP
改变javascript函数内部this指针指向的三种方法
2010/04/23 Javascript
JS的get和set使用示例
2014/02/20 Javascript
ActiveX控件与Javascript之间的交互示例
2014/06/04 Javascript
setTimeout()递归调用不加引号出错的解决方法
2014/09/05 Javascript
javascript的函数劫持浅析
2016/09/26 Javascript
微信小程序 tabs选项卡效果的实现
2017/01/05 Javascript
javascript实现日期三级联动下拉框选择菜单
2020/12/03 Javascript
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
基于vue.js的分页插件详解
2017/11/27 Javascript
详解webpack-dev-server的简单使用
2018/04/02 Javascript
解决vue跨域axios异步通信问题
2019/04/17 Javascript
微信小程序调用微信支付接口的实现方法
2019/04/29 Javascript
js获取url页面id,也就是最后的数字文件名
2020/09/25 Javascript
[02:41]DOTA2英雄基础教程 冥魂大帝
2014/01/16 DOTA
[03:04]2018年度DOTA2玩家最喜爱的主播-完美盛典
2018/12/16 DOTA
在Python中使用M2Crypto模块实现AES加密的教程
2015/04/08 Python
Python合并字符串的3种方法
2015/05/21 Python
python实现支持目录FTP上传下载文件的方法
2015/06/03 Python
tensorflow实现简单的卷积网络
2018/05/24 Python
Python根据指定日期计算后n天,前n天是哪一天的方法
2018/05/29 Python
python-pyinstaller、打包后获取路径的实例
2019/06/10 Python
Win10里python3创建虚拟环境的步骤
2020/01/31 Python
python实现发送带附件的邮件代码分享
2020/09/22 Python
俄罗斯品牌服装在线商店:VIPAVENUE
2020/08/10 全球购物
惠而浦美国官网:Whirlpool.com
2021/01/19 全球购物
自考生自我评价分享
2014/01/18 职场文书
英语故事演讲稿
2014/04/29 职场文书
公务员试用期满考核材料
2014/05/22 职场文书
导游词欢迎词
2015/02/02 职场文书
JS异步堆栈追踪之为什么await胜过Promise
2021/04/28 Javascript
Win10加载疑难解答时出错发生意外错误的解决方法
2022/07/07 数码科技