我们从Python开源项目中,提取了以下13个代码示例,用于说明如何使用zmq.ContextTerminated()。
def mainloop_recv(self): try: while True: if self._frsock.closed: break msg = loadb(self._frsock.recv(copy=False).bytes) identifier, type, payload = msg self._dispatcher.dispatch(type, self, identifier, payload) except zmq.ContextTerminated: pass except zmq.ZMQError as e: if self._tosock.closed: logger.warn('Recv socket closed unexpectedly.') else: raise e
def atest_ctxterm(self): s = self.context.socket(zmq.REP) t = Thread(target=self.context.term) t.start() self.assertRaises(ContextTerminated, s.recv, zmq.NOBLOCK) self.assertRaisesErrno(zmq.TERM, s.recv, zmq.NOBLOCK) s.close() t.join()
def get_data(self): try: for k in itertools.count(): if self._size > 0 and k >= self._size: break dp = loads(self.socket.recv(copy=False).bytes) yield dp except zmq.ContextTerminated: logger.info("ContextTerminated in Master Prefetch Process") return except BaseException: raise
def recv(self): try: return loadb(self._sock.recv(copy=False).bytes) except zmq.ContextTerminated: pass
def mainloop_send(self): try: while True: job = self._send_queue.get() self._sock.send(dumpb(job), copy=False) except zmq.ContextTerminated: pass
def mainloop_send(self): try: while True: if self._tosock.closed: break job = self._send_queue.get() self._tosock.send_multipart([job.identifier, dumpb(job.payload)], copy=False) except zmq.ContextTerminated: pass except zmq.ZMQError as e: if self._tosock.closed: logger.warn('Send socket closed unexpectedly.') else: raise e
def receive_message(socket, blocking=True): flags = 0 if blocking else zmq.NOBLOCK try: cmd, data = socket.recv_multipart(flags=flags) return cmd, data except zmq.Again: return None, None except zmq.ContextTerminated: print("Context terminated ..") return None, None except KeyboardInterrupt: return None, None
def _run (self): # socket must be created on the same thread self.socket.setsockopt(zmq.SUBSCRIBE, b'') self.socket.setsockopt(zmq.RCVTIMEO, 5000) self.socket.connect(self.tr) got_data = False self.monitor.reset() while self.active: try: with self.monitor: line = self.socket.recv_string() self.monitor.on_recv_msg(line) self.last_data_recv_ts = time.time() # signal once if not got_data: self.event_handler.on_async_alive() got_data = True # got a timeout - mark as not alive and retry except zmq.Again: # signal once if got_data: self.event_handler.on_async_dead() got_data = False continue except zmq.ContextTerminated: # outside thread signaled us to exit assert(not self.active) break msg = json.loads(line) name = msg['name'] data = msg['data'] type = msg['type'] baseline = msg.get('baseline', False) self.raw_snapshot[name] = data self.__dispatch(name, type, data, baseline) # closing of socket must be from the same thread self.socket.close(linger = 0)