激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - zookeeper python接口實(shí)例詳解

zookeeper python接口實(shí)例詳解

2021-01-06 00:19swpihchj Python

這篇文章主要介紹了zookeeper python接口實(shí)例詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文主要講python支持zookeeper接口庫(kù)安裝和使用。zk的python接口庫(kù)有zkpython,還有kazoo,下面是zkpython,是基于zk的C庫(kù)的python接口。

zkpython安裝

前提是zookeeper安裝包已經(jīng)在/usr/local/zookeeper下

?
1
2
3
4
5
6
7
8
9
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應(yīng)用

下面是網(wǎng)上一個(gè)zkpython的類(lèi),用的時(shí)候只要import進(jìn)去就行
vim zkclient.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/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)

總結(jié)

以上就是本文關(guān)于zookeeper python接口實(shí)例詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

原文鏈接:http://blog.csdn.net/swpihchj/article/details/24603641

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 午夜视频在线观看91 | 午夜视频在线观 | 男女无遮挡羞羞视频 | 免费在线观看亚洲 | 亚洲天堂岛国片 | 亚洲精品a在线观看 | 午夜激情视频网站 | 国产女同玩人妖 | 姑娘第四集免费看视频 | 国产妇女乱码一区二区三区 | 亚洲一区在线免费视频 | 性欧美视频在线观看 | 黄片一级毛片 | 国产成人在线播放视频 | 久久久久久久不卡 | 日韩视频中文 | 欧美三级欧美成人高清www | 日韩视频在线观看免费视频 | 国产免费黄网 | 欧美一级三级在线观看 | 成人444kkkk在线观看 | 国产精品一区久久久久 | 国产毛片在线 | 二区三区四区 | 欧美人与牲禽动交精品一区 | 91精品最新国内在线播放 | 31freehdxxxx欧美 | 精品一区二区三区在线播放 | 久久蜜桃香蕉精品一区二区三区 | 精品国产一区二区三区四区阿崩 | 一级做a爱片毛片免费 | 亚洲一区中文字幕 | 手机免费看一级片 | 成人午夜在线免费视频 | 欧美一区二区黄色片 | 一区二区三区在线观看免费视频 | 久久久久9999| 国产精品视频2021 | 欧美四级在线观看 | 中文字幕在线免费播放 | 欧美一级全黄 |