def go(): c = QueueClient('127.0.0.1', 9876) c.connect() for i in range(0, 500): c.send(m) c.recv() time.sleep(0.1) c.disconnect()
# -*- coding: utf-8 -*- if __name__ == '__main__': from bucker.api.message import Message s = """<qs xmlns="http://purl.oclc.org/DEFUZE/qs" resp="1e582a22b3f82a7c9acc227b0ccb6fdec0d84e6b74dccbcb542e8d11e1b379dd" type="response"><op><push-message /></op><qid>event</qid><mid>3664f5666c2564f7fb6c16d45b1e5f7f951ebff6d7d34792fe4badb63b566ed2</mid><payload>PGxsdXA6bm90aWZpY2F0aW9uIHhtbG5zOmxsdXA9Imh0dHA6Ly93d3cueDJ4Mngub3JnL2xsdXAiIGFjdGlvbj0iIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwNS9BdG9tIj48bGx1cDpyZWNpcGllbnQgaHJlZj0iIiAvPjxsaW5rIGhyZWY9Imh0dHA6Ly9hdG9tcHViLmRlZnV6ZS5vcmcvc3RhcnRlci9zYWxhZC9OYXBvbGl0YW4uYXRvbSIgcmVsPSJzZWxmIiB0eXBlPSJhcHBsaWNhdGlvbi9hdG9tK3htbDt0eXBlPWVudHJ5IiAvPjxjYXRlZ29yeSB0ZXJtPSJpbmRpZSIgLz48Y2F0ZWdvcnkgdGVybT0icm9jayIgLz48Y2F0ZWdvcnkgdGVybT0iamF6eiIgLz48Y2F0ZWdvcnkgdGVybT0iYmx1ZXMiIC8+PC9sbHVwOm5vdGlmaWNhdGlvbj4=</payload></qs>""" m = Message.parse(s) from bucker.api.client import QueueClient q = QueueClient('127.0.0.1', 9876) q.connect() q.send(m) print q.recv() q.disconnect()
class QueueHandler(threading.Thread): def __init__(self, qid, host, port): """ A QueueHandler is just a central point for the blog to handle messages pushed to a given queue_id. This avoids duplication of code and make the blog more robust. The ``qid`` is a queue identifier that will created (if not yet in the queue server) when the register() method is called. The ``host`` and ``port`` are the address of the queue server to connect to. Each handler runs in its own thread. So for instance: q = QueueHandler('event', '127.0.0.1', 9876) q.register() #initializes and connects to the queue server q.start() # start the thread q.process(message) ... q.unregister() q.join() """ threading.Thread.__init__(self) self.qid = qid self.client = QueueClient(host, port) self.running = False self._messages = Queue() def register(self): self.running = True self.client.connect() m = NewQueue() m.qid = self.qid.encode('utf-8') self.process(m) def unregister(self): self.running = False self.process(None) self.client.disconnect() def process(self, message): """ Ask the handler to process the message. The message is poyt into a thread-safe container and processed by the queue handler as soon as it is possible. Therefore the caller doesn't get blocked on this call. """ self._messages.put(message) def run(self): """ This method runs into the thread until the handler is unregistered. """ while self.running: m = None try: m = self._messages.get() except Empty: continue if m != None: self.client.send(m) try: # we are not interested in the response but we free # the associated resources on the queue then self.client.recv() except socket.timeout: pass