zoukankan      html  css  js  c++  java
  • python3 连接 zookeeper

    zookeeper的增 删 改 查 watch监听。

    from  kazoo.client import KazooClient
    import time,os
    import timeit
    os.chdir(os.getcwd())
    def connection():
        zk=KazooClient('localhost:2181')
        zk.start()
        #print(zk.connected)
        if zk.connected == "True":
            kk = "success"
        else:
            kk = "failed"
        print(zk.connected)
        return zk.connected
    def Utime(f):
        def timechange(*args,**kwargs):
            start_time=time.time()
            f(*args,**kwargs)
            end_time=time.time()
            execution_time=(end_time-start_time)*1000
            return execution_time
        return timechange
    
    #增
    @Utime
    def create(hosts,path,data):
        zk = KazooClient(hosts)
        zk.start()
        value=data.encode()
        zk.create(path,value,makepath=True)
        zk.stop()
    
    #删
    @Utime
    def delete(hosts,path):
        zk1=KazooClient(hosts)
        zk1.start()
        zk1.delete(path)
        zk1.stop()
    
    #查
    @Utime
    def get(hosts,path):
        zk2=KazooClient(hosts)
        zk2.start()
        zk2.get(path)
        zk2.stop()
     #   return data
    
    #改
    @Utime
    def set(hosts,path,data):
        zk3=KazooClient(hosts)
        zk3.start()
        value=data.encode()
        zk3.set(path,value)
        zk3.stop()
    #递归删
    def delete_all(hosts,path):
        zk4=KazooClient(hosts)
        zk4.start(timeout=10)
        zk4.delete(path,recursive=True)
        zk4.stop()
    
    #watch
    def node_Watch(host,path):
        zk=KazooClient(host)
        zk.start()
        @zk.DataWatch(path)
        def my_change(data, stat):
            time.sleep(3)
            #print("Data is %s" % data)
            #print("Version is %s" % stat.version)
            #print("Event is %s" % event)
        while True:
            time.sleep(3)
            #print("OK")
    

      

  • 相关阅读:
    数据库第三范式的思考
    channel通道例子
    go 测试代码性能实例
    go 新建項目引入gin失敗
    go 创建切片slice的四种方法
    Hibernate查询操作
    shell 分割训练数据
    hadoop streaming 分桶到不同的part
    C语言调用另一个文件的方法
    在springboot中使用jdbcTemplate(3)
  • 原文地址:https://www.cnblogs.com/cyanrose/p/11882963.html
Copyright © 2011-2022 走看看