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()
  • 相关阅读:
    远程诊断DoIP
    基于linux内核包过滤技术的应用网关
    Boost内存池使用与测试
    C++ 编程规范
    大象——Thinking in UML
    C++ 创建类时常考虑的问题
    SLIP—串行线路上传输数据报的非标准协议
    神秘的程序员——编程的乐趣
    Bad Smell (代码的坏味道)
    模式与软件架构——软件架构的非功能特征
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/3803544.html
Copyright © 2011-2022 走看看