国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Zookeeper接口kazoo實例解析

2020-02-22 22:56:30
字體:
來源:轉載
供稿:網友

本文主要研究的是Zookeeper接口kazoo的相關內容,具體介紹如下。

zookeeper的開發接口以前主要以java和c為主,隨著python項目越來越多的使用zookeeper作為分布式集群實現,python的zookeeper接口也出現了很多,現在主流的純python的zookeeper接口是kazoo。因此如何使用kazoo開發基于python的分布式程序是必須掌握的。

1.安裝kazoo

yum install python-pippip install kazoo

安裝過程中會出現一些python依賴包未安裝的情況,安裝即可。

2.運行kazoo基礎例子kazoo_basic.py

import timefrom kazoo.client import KazooClientfrom kazoo.client import KazooStatedef 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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 九龙县| 顺义区| 五台县| 通州区| 河西区| 抚松县| 上高县| 新化县| 固镇县| 青冈县| 西昌市| 武定县| 磐石市| 汕头市| 崇文区| 浪卡子县| 宁晋县| 正定县| 葫芦岛市| 宁晋县| 玉林市| 宁安市| 藁城市| 铜川市| 上虞市| 韶关市| 陆川县| 宽甸| 太和县| 荆门市| 玉山县| 新沂市| 连城县| 临澧县| 确山县| 高雄市| 龙里县| 建平县| 福清市| 奉节县| 南充市|