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的Flask框架中实现分页功能的教程
Apr 20 Python
Python 如何访问外围作用域中的变量
Sep 11 Python
Python使用requests发送POST请求实例代码
Jan 25 Python
用python 批量更改图像尺寸到统一大小的方法
Mar 31 Python
python调用API实现智能回复机器人
Apr 10 Python
Python实现的生产者、消费者问题完整实例
May 30 Python
python3 kmp 字符串匹配的方法
Jul 07 Python
基于python实现KNN分类算法
Apr 23 Python
python 多个参数不为空校验方法
Feb 14 Python
Python实现定制自动化业务流量报表周报功能【XlsxWriter模块】
Mar 11 Python
详解python解压压缩包的五种方法
Jul 05 Python
Windows下pycharm创建Django 项目(虚拟环境)过程解析
Sep 16 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完整的日历类(CLASS)
2006/11/27 PHP
模拟flock实现文件锁定
2007/02/14 PHP
浅谈PDO的rowCount函数
2015/06/18 PHP
Thinkphp开发--集成极光推送
2017/09/15 PHP
PHP5中使用mysqli的prepare操作数据库的介绍
2019/03/18 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
2019/11/24 PHP
初学JavaScript第二章
2008/09/30 Javascript
图片动画横条广告带上下滚动的JS代码
2013/10/25 Javascript
jQuery实现提交按钮点击后变成正在处理字样并禁止点击的方法
2015/03/24 Javascript
谈谈对offsetleft兼容性的理解
2015/11/11 Javascript
JQuery用户名校验的具体实现
2016/03/18 Javascript
深入理解JavaScript中为什么string可以拥有方法
2016/05/24 Javascript
Backbone中View之间传值的学习心得
2016/08/09 Javascript
jQuery实现的无缝广告图片左右滚动功能详解
2016/12/24 Javascript
jquery滚动条插件slimScroll使用方法
2017/02/09 Javascript
nodejs中模块定义实例详解
2017/03/18 NodeJs
HTML5+Canvas调用手机拍照功能实现图片上传(下)
2017/04/21 Javascript
详解vue-cli + webpack 多页面实例应用
2017/04/25 Javascript
d3.js入门教程之数据绑定详解
2017/04/28 Javascript
js实现随机点名系统(实例讲解)
2017/10/18 Javascript
Fundebug支持监控微信小程序HTTP请求错误的方法
2019/02/21 Javascript
qrcode生成二维码微信长按无法识别问题的解决
2019/04/04 Javascript
基于Vue实现电商SKU组合算法问题
2019/05/29 Javascript
从零撸一个pc端vue的ui组件库( 计数器组件 )
2019/08/08 Javascript
python创建只读属性对象的方法(ReadOnlyObject)
2013/02/10 Python
Python ORM框架SQLAlchemy学习笔记之数据查询实例
2014/06/10 Python
Python实现代码统计工具(终极篇)
2016/07/04 Python
python语言中with as的用法使用详解
2018/02/23 Python
Python设计模式之建造者模式实例详解
2019/01/17 Python
打包PyQt5应用时的注意事项
2020/02/14 Python
Python enumerate() 函数如何实现索引功能
2020/06/29 Python
python+selenium实现12306模拟登录的步骤
2021/01/21 Python
项目总经理岗位职责
2014/02/14 职场文书
2014教师党员自我评议(5篇)
2014/09/20 职场文书
总结Pyinstaller打包的高级用法
2021/06/28 Python
js前端面试常见浏览器缓存强缓存及协商缓存实例
2022/06/21 Javascript