zoukankan      html  css  js  c++  java
  • Python connect zookeeper use the kazoo module

    doc:http://kazoo.readthedocs.org/en/latest/basic_usage.html

    eg:
    from kazoo.client import KazooClient
    
    zk = KazooClient()
    zk.start()
    #!/bin/env python
    
    #Zookeeper test
    #Date 2013-03-11
    
    import sys
    sys.path.append('/global/exec/weizhong/lib')
    import time
    from kazoo.client import KazooClient
    
    #init the client connect tb055 zookeeper server
    zk = KazooClient('tb055:2181',10.0)
    zk.start()
    
    #lock =zk.Lock('/weidata')
    
    #get the list 
    children = zk.get_children('/')
    print children
    #exec ruok command
    ruok = zk.command('ruok')
    print ruok
    #get the version of the zookeeper server 
    version = zk.server_version()
    print version
    #create
    if not zk.exists('/weizhong'):
    	zk.create('/weizhong',b'Welcome to the servr on tb055')
    print zk.exists('/weizhong1')
    data,state = zk.get('/weizhong')
    
    print data
    print state
    
    
    #stop the session
    #zk.stop()
    #watcher test
    #当改变时调用
    @zk.ChildrenWatch("/")
    def watch_children(children):
        print("Children are now: %s" % children)
        time.sleep(5)
    # Above function called immediately, and from then on
    
    @zk.DataWatch("/weidata")
    def watch_node(data, stat):
        print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))
    
    
    def my_func(event):
        #check to see what the children are now
        print 30*'-'
    
    # Call my_func when the children change
    children = zk.get_children("/", watch=my_func)
    zk.stop()
  • 相关阅读:
    Unity Animation扩展方法总结
    Unity 离线建造系统
    Unity 任意区域截屏创建Sprite
    Unity ugui拖动控件(地图模式与物件模式)
    Unity 极简UI框架
    Unity 芯片拼图算法
    Unity Procedural Level Generator 基础总结与功能优化
    MANIFEST.MF是个什么?
    外包程序员怎么办?
    文件上传transferTo一行代码的bug
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/3803544.html
Copyright © 2011-2022 走看看