本文主要讲python支持zookeeper的接口库安装和使用。zk的python接口库有zkpython,还有kazoo,下面是zkpython,是基于zk的C库的python接口。
zkpython安装
前提是zookeeper安装包已经在/usr/local/zookeeper下
cd /usr/local/zookeeper/src/c ./configure make make install wget --no-check-certificate http://pypi.python.org/packages/source/z/zkpython/zkpython-0.4.tar.gz tar -zxvf zkpython-0.4.tar.gz cd zkpython-0.4 sudo python setup.py install
zkpython应用
下面是网上一个zkpython的类,用的时候只要import进去就行
vim zkclient.py
#!/usr/bin/env python2.7 # -*- coding: UTF-8 -*- import zookeeper, time, threading from collections import namedtuple DEFAULT_TIMEOUT = 30000 VERBOSE = True ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"} # Mapping of connection state values to human strings. STATE_NAME_MAPPING = { zookeeper.ASSOCIATING_STATE: "associating", zookeeper.AUTH_FAILED_STATE: "auth-failed", zookeeper.CONNECTED_STATE: "connected", zookeeper.CONNECTING_STATE: "connecting", zookeeper.EXPIRED_SESSION_STATE: "expired", } # Mapping of event type to human string. TYPE_NAME_MAPPING = { zookeeper.NOTWATCHING_EVENT: "not-watching", zookeeper.SESSION_EVENT: "session", zookeeper.CREATED_EVENT: "created", zookeeper.DELETED_EVENT: "deleted", zookeeper.CHANGED_EVENT: "changed", zookeeper.CHILD_EVENT: "child", } class ZKClientError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class ClientEvent(namedtuple("ClientEvent", 'type, connection_state, path')): """ A client event is returned when a watch deferred fires. It denotes some event on the zookeeper client that the watch was requested on. """ @property def type_name(self): return TYPE_NAME_MAPPING[self.type] @property def state_name(self): return STATE_NAME_MAPPING[self.connection_state] def __repr__(self): return "<ClientEvent %s at %r state: %s>" % ( self.type_name, self.path, self.state_name) def watchmethod(func): def decorated(handle, atype, state, path): event = ClientEvent(atype, state, path) return func(event) return decorated class ZKClient(object): def __init__(self, servers, timeout=DEFAULT_TIMEOUT): self.timeout = timeout self.connected = False self.conn_cv = threading.Condition( ) self.handle = -1 self.conn_cv.acquire() if VERBOSE: print("Connecting to %s" % (servers)) start = time.time() self.handle = zookeeper.init(servers, self.connection_watcher, timeout) self.conn_cv.wait(timeout/1000) self.conn_cv.release() if not self.connected: raise ZKClientError("Unable to connect to %s" % (servers)) if VERBOSE: print("Connected in %d ms, handle is %d" % (int((time.time() - start) * 1000), self.handle)) def connection_watcher(self, h, type, state, path): self.handle = h self.conn_cv.acquire() self.connected = True self.conn_cv.notifyAll() self.conn_cv.release() def close(self): return zookeeper.close(self.handle) def create(self, path, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]): start = time.time() result = zookeeper.create(self.handle, path, data, acl, flags) if VERBOSE: print("Node %s created in %d ms" % (path, int((time.time() - start) * 1000))) return result def delete(self, path, version=-1): start = time.time() result = zookeeper.delete(self.handle, path, version) if VERBOSE: print("Node %s deleted in %d ms" % (path, int((time.time() - start) * 1000))) return result def get(self, path, watcher=None): return zookeeper.get(self.handle, path, watcher) def exists(self, path, watcher=None): return zookeeper.exists(self.handle, path, watcher) def set(self, path, data="", version=-1): return zookeeper.set(self.handle, path, data, version) def set2(self, path, data="", version=-1): return zookeeper.set2(self.handle, path, data, version) def get_children(self, path, watcher=None): return zookeeper.get_children(self.handle, path, watcher) def async(self, path = "/"): return zookeeper.async(self.handle, path) def acreate(self, path, callback, data="", flags=0, acl=[ZOO_OPEN_ACL_UNSAFE]): result = zookeeper.acreate(self.handle, path, data, acl, flags, callback) return result def adelete(self, path, callback, version=-1): return zookeeper.adelete(self.handle, path, version, callback) def aget(self, path, callback, watcher=None): return zookeeper.aget(self.handle, path, watcher, callback) def aexists(self, path, callback, watcher=None): return zookeeper.aexists(self.handle, path, watcher, callback) def aset(self, path, callback, data="", version=-1): return zookeeper.aset(self.handle, path, data, version, callback) watch_count = 0 """Callable watcher that counts the number of notifications""" class CountingWatcher(object): def __init__(self): self.count = 0 global watch_count self.id = watch_count watch_count += 1 def waitForExpected(self, count, maxwait): """Wait up to maxwait for the specified count, return the count whether or not maxwait reached. Arguments: - `count`: expected count - `maxwait`: max milliseconds to wait """ waited = 0 while (waited < maxwait): if self.count >= count: return self.count time.sleep(1.0); waited += 1000 return self.count def __call__(self, handle, typ, state, path): self.count += 1 if VERBOSE: print("handle %d got watch for %s in watcher %d, count %d" % (handle, path, self.id, self.count)) """Callable watcher that counts the number of notifications and verifies that the paths are sequential""" class SequentialCountingWatcher(CountingWatcher): def __init__(self, child_path): CountingWatcher.__init__(self) self.child_path = child_path def __call__(self, handle, typ, state, path): if not self.child_path(self.count) == path: raise ZKClientError("handle %d invalid path order %s" % (handle, path)) CountingWatcher.__call__(self, handle, typ, state, path) class Callback(object): def __init__(self): self.cv = threading.Condition() self.callback_flag = False self.rc = -1 def callback(self, handle, rc, handler): self.cv.acquire() self.callback_flag = True self.handle = handle self.rc = rc handler() self.cv.notify() self.cv.release() def waitForSuccess(self): while not self.callback_flag: self.cv.wait() self.cv.release() if not self.callback_flag == True: raise ZKClientError("asynchronous operation timed out on handle %d" % (self.handle)) if not self.rc == zookeeper.OK: raise ZKClientError( "asynchronous operation failed on handle %d with rc %d" % (self.handle, self.rc)) class GetCallback(Callback): def __init__(self): Callback.__init__(self) def __call__(self, handle, rc, value, stat): def handler(): self.value = value self.stat = stat self.callback(handle, rc, handler) class SetCallback(Callback): def __init__(self): Callback.__init__(self) def __call__(self, handle, rc, stat): def handler(): self.stat = stat self.callback(handle, rc, handler) class ExistsCallback(SetCallback): pass class CreateCallback(Callback): def __init__(self): Callback.__init__(self) def __call__(self, handle, rc, path): def handler(): self.path = path self.callback(handle, rc, handler) class DeleteCallback(Callback): def __init__(self): Callback.__init__(self) def __call__(self, handle, rc): def handler(): pass self.callback(handle, rc, handler)
总结
以上就是本文关于zookeeper python接口实例详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
本文向大家介绍java 接口回调实例详解,包括了java 接口回调实例详解的使用技巧和注意事项,需要的朋友参考一下 java 接口回调实例详解 首先官方对接口回调的定义是这样的,所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法。这样听起来有点绕,我们可以这么理解接口回调:比如我们想知道隔壁老王啥时候回家?但是我们有自己的事情做不能一直监视着老王
本文向大家介绍java中的interface接口实例详解,包括了java中的interface接口实例详解的使用技巧和注意事项,需要的朋友参考一下 java中的interface接口实例详解 接口:Java接口是一些方法表征的集合,但是却不会在接口里实现具体的方法。 java接口的特点如下: 1、java接口不能被实例化 2、java接口中声明的成员自动被设置为public,所以不存在priva
本文向大家介绍python图形用户接口实例详解,包括了python图形用户接口实例详解的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python图形用户接口实例的具体代码,供大家参考,具体内容如下 运用tkinter图形库,模拟聊天应用界面,实现信息发送. 界面效果如下: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。
本文向大家介绍对python调用RPC接口的实例详解,包括了对python调用RPC接口的实例详解的使用技巧和注意事项,需要的朋友参考一下 要调用RPC接口,python提供了一个框架grpc,这是google开源的 rpc相关文档: https://grpc.io/docs/tutorials/basic/python.html 需要安装的python包如下: 1.grpc安装 pip inst
本文向大家介绍python+requests+unittest API接口测试实例(详解),包括了python+requests+unittest API接口测试实例(详解)的使用技巧和注意事项,需要的朋友参考一下 我在网上查找了下接口测试相关的资料,大都重点是以数据驱动的形式,将用例维护在文本或表格中,而没有说明怎么样去生成想要的用例, 问题: 测试接口时,比如参数a,b,c,我要先测a参数,有
本文向大家介绍C语言接口与实现方法实例详解,包括了C语言接口与实现方法实例详解的使用技巧和注意事项,需要的朋友参考一下 本文以实例形式详细讲述了C语言接口与实现方法,对于深入掌握C语言程序设计有一定的借鉴价值。分享给大家供大家参考。具体分析如下: 一般来说,一个模块有两部分组成:接口和实现。接口指明模块要做什么,它声明了使用该模块的代码可用的标识符、类型和例程,实现指明模块是如何完成其接口声明的目
问题内容: 扩展初始化接口时提出的问题?),我们在实例化接口的同时用实现的类对其进行初始化。 我的问题是,为什么首先要使用Interface实例化它?为什么我不能直接用实现的类实例化它?例如。: Doc是接口,而SimpleDoc正在实现它。SimpleDoc有什么问题?mydoc = new SimpleDoc(); 哪里会失败? 问题答案: 通常,最好的方法是依赖系统中的抽象类型(接口或抽象类
本文向大家介绍详解Java中接口的定义与实例代码,包括了详解Java中接口的定义与实例代码的使用技巧和注意事项,需要的朋友参考一下 Java中接口的定义详解 1、定义接口 使用interface来定义一个接口。接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成。定义接口的基本格式如下: 修饰符:可选,用于指定接口的访问权限,可选值为public。如