def __init__(self, ip='127.0.0.1'): c = zmq._Context() self.ip = ip self.producer = c.socket(zmq.PUSH) self.sink = c.socket(zmq.PULL) self.producer_port = self.producer.bind_to_random_port('tcp://%s' % self.ip) self.sink_port = self.sink.bind_to_random_port('tcp://%s' % self.ip)
def start(self): """Start the consumer. This starts a listen loop on a zmq.PULL socket, calling ``self.handle`` on each incoming request and pushing the response on a zmq.PUSH socket back to the producer.""" if not self.initialized: raise Exception("Consumer not initialized (no Producer).") producer = self.producer context = zmq._Context() self.pull = context.socket(zmq.PULL) self.push = context.socket(zmq.PUSH) self.pull.connect('tcp://%s:%s' % (producer.host, producer.push_port)) self.push.connect('tcp://%s:%s' % (producer.host, producer.pull_port)) # TODO: notify the producer that this consumer's ready for work? self.listen()
def run(self): context = zmq._Context() producer = context.socket(zmq.PULL) sink = context.socket(zmq.PUSH) producer.connect('tcp://%s:%s' % (self.ip, self.producer)) sink.connect('tcp://%s:%s' % (self.ip, self.sink)) while True: task = producer.recv() if task == "shutdown": sink.send("Shutting down pid %s" % (os.getpid())) eventlet.sleep(0) return sink.send("Task: %s (%s)" % (task, os.getpid()))
def get_context(self, io_threads=1): """zmq's Context must be unique within a hub The zeromq API documentation states: All zmq sockets passed to the zmq_poll() function must share the same zmq context and must belong to the thread calling zmq_poll() As zmq_poll is what's eventually being called then we need to insure that all sockets that are going to be passed to zmq_poll (via hub.do_poll) are in the same context """ try: return _threadlocal.context except AttributeError: _threadlocal.context = zmq._Context(io_threads) return _threadlocal.context