Skip to content
Find file
db8a3bf May 28, 2009
86 lines (76 sloc) 2.98 KB
import zookeeper, threading, sys
ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"};
class ZooKeeperQueue(object):
def __init__(self,queuename):
self.connected = False
self.queuename = "/" + queuename = threading.Condition()
def watcher(handle,type,state,path):
print "Connected"
self.connected = True
self.handle = zookeeper.init("localhost:2181", watcher, 10000, 0)
if not self.connected:
print "Connection to ZooKeeper cluster timed out - is a server running on localhost:2181?"
zookeeper.create(self.handle,self.queuename,"queue top level", [ZOO_OPEN_ACL_UNSAFE],0)
except IOError, e:
if e.message == zookeeper.zerror(zookeeper.NODEEXISTS):
print "Queue already exists"
raise e
def enqueue(self,val):
zookeeper.create(self.handle, self.queuename+"/item", val, [ZOO_OPEN_ACL_UNSAFE],zookeeper.SEQUENCE)
def dequeue(self):
while True:
children = sorted(zookeeper.get_children(self.handle, self.queuename,None))
if len(children) == 0:
return None
for child in children:
data = self.get_and_delete(self.queuename + "/" + children[0])
if data:
return data
def get_and_delete(self,node):
(data,stat) = zookeeper.get(self.handle, node, None)
zookeeper.delete(self.handle, node, stat["version"])
return data
except IOError, e:
if e.message == zookeeper.zerror(zookeeper.NONODE):
return None
raise e
def block_dequeue(self):
def queue_watcher(handle,event,state,path):
while True:
children = sorted(zookeeper.get_children(self.handle, self.queuename, queue_watcher))
for child in children:
data = self.get_and_delete(self.queuename+"/"+children[0])
if data != None:
return data
if __name__ == '__main__':
zk = ZooKeeperQueue("myfirstqueue")
print "Enqueuing three items"
zk.enqueue("queue item 1")
zk.enqueue("queue item 2")
zk.enqueue("queue item 3")
print "Done"
print "Consuming all items in queue"
v = zk.dequeue()
while v != None:
print v
v = zk.dequeue()
print "Done"
Something went wrong with that request. Please try again.