def _test_pinging(self): ctx = zmq.Context() locker = threading.RLock() socket = ctx.socket(zmq.REQ) socket.connect(_ENDPOINT) ponger = Ponger(ctx) failures = [0] def failed(): failures[0] += 1 ponger.start() time.sleep(0.2) pinger = Pinger('me', socket, locker, failed, duration=0.1) pinger.start() time.sleep(0.2) ponger.join() ctx.destroy(0) pinger.stop() # let's see how many ping we did self.assertEqual(ponger.count, 5)
class Worker(object): def __init__(self, endpoint, identity, target, timeout=1.): self.identity = identity if identity.startswith('ipc'): register_ipc_file(identity) self.ctx = zmq.Context() self.timeout = timeout * 1000 self.master = self.ctx.socket(zmq.REQ) self.master.connect(endpoint) self.work = self.ctx.socket(zmq.REP) self.work.bind(identity) self.registered = self.running = False # setting a poller self.poller = zmq.Poller() self.poller.register(self.work, zmq.POLLIN) self.locker = threading.RLock() self.pinger = Pinger(self.identity, self.master, self.locker, self.failed) self.target = target def failed(self): logger.debug("ping failed let's die") try: self._msg('REMOVE', 'REMOVED') except RegisterError: pass self.stop() def register(self): self._msg('PING', 'PONG') self.registered = True def _msg(self, req, rep): self.pinger.disable() try: with self.locker: poller = zmq.Poller() poller.register(self.master, zmq.POLLIN) # ping the master we are online, with an ID try: data = serialize(req, self.identity) self.master.send(data, zmq.NOBLOCK) except zmq.ZMQError, e: raise RegisterError(str(e)) try: events = dict(poller.poll(self.timeout)) except zmq.ZMQError, e: raise RegisterError(str(e)) if events == {}: raise RegisterError("Nothing came back") else: for socket in events: res = socket.recv() if res != rep: raise RegisterError(res)
class Worker(object): def __init__(self, endpoint, identity, target, timeout=1.): self.identity = identity self.ctx = zmq.Context() self.timeout = timeout * 1000 self.master = self.ctx.socket(zmq.REQ) self.master.connect(endpoint) self.work = self.ctx.socket(zmq.REP) self.work.bind(identity) self.registered = self.running = False # setting a poller self.poller = zmq.Poller() self.poller.register(self.work, zmq.POLLIN) self.locker = threading.RLock() self.pinger = Pinger(self.identity, self.master, self.locker, self.failed) self.target = target def failed(self): logger.debug("ping failed let's die") try: self._msg('REMOVE', 'REMOVED') except RegisterError: pass self.stop() def register(self): self._msg('PING', 'PONG') self.registered = True def _msg(self, req, rep): self.pinger.disable() try: with self.locker: poller = zmq.Poller() poller.register(self.master, zmq.POLLIN) # ping the master we are online, with an ID try: data = serialize(req, self.identity) self.master.send(data, zmq.NOBLOCK) except zmq.ZMQError, e: raise RegisterError(str(e)) try: events = dict(poller.poll(self.timeout)) except zmq.ZMQError, e: raise RegisterError(str(e)) if events == {}: raise RegisterError("Nothing came back") else: for socket in events: res = socket.recv() if res != rep: raise RegisterError(res)
def __init__(self, endpoint, identity, target, timeout=1.): self.identity = identity self.ctx = zmq.Context() self.timeout = timeout * 1000 self.master = self.ctx.socket(zmq.REQ) self.master.connect(endpoint) self.work = self.ctx.socket(zmq.REP) self.work.bind(identity) self.registered = self.running = False # setting a poller self.poller = zmq.Poller() self.poller.register(self.work, zmq.POLLIN) self.locker = threading.RLock() self.pinger = Pinger(self.identity, self.master, self.locker, self.failed) self.target = target
def test_too_many_failures(self): ctx = zmq.Context() locker = threading.RLock() socket = ctx.socket(zmq.REQ) socket.connect(_ENDPOINT) failures = [0] def failed(): failures[0] += 1 pinger = Pinger('me', socket, locker, failed, duration=0.1) pinger.start() time.sleep(1.) # pinging 10 times nowwhere will make the pinger quit #pinger.join() ctx.destroy(0) self.assertTrue(len(failures), 10) self.assertTrue(pinger.unresponsive)