Python zmq.POLLIN Examples
The following are 30
code examples of zmq.POLLIN().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
zmq
, or try the search function
.
Example #1
Source File: test_poll.py From vnpy_crypto with MIT License | 6 votes |
def test_timeout(self): """make sure Poller.poll timeout has the right units (milliseconds).""" s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) poller = self.Poller() poller.register(s1, zmq.POLLIN) tic = time.time() evt = poller.poll(.005) toc = time.time() self.assertTrue(toc-tic < 0.1) tic = time.time() evt = poller.poll(5) toc = time.time() self.assertTrue(toc-tic < 0.1) self.assertTrue(toc-tic > .001) tic = time.time() evt = poller.poll(500) toc = time.time() self.assertTrue(toc-tic < 1) self.assertTrue(toc-tic > 0.1)
Example #2
Source File: datafeed.py From cryptotrader with MIT License | 6 votes |
def __init__(self, exchange='', addr='ipc:///tmp/feed.ipc', timeout=30): """ :param period: int: Data sampling period :param pairs: list: Pair symbols to trade :param exchange: str: FeedDaemon exchange to query :param addr: str: Client socked address :param timeout: int: """ super(DataFeed, self).__init__() # Sock objects self.context = zmq.Context() self.addr = addr self.exchange = exchange self.timeout = timeout * 1000 self.sock = self.context.socket(zmq.REQ) self.sock.connect(addr) self.poll = zmq.Poller() self.poll.register(self.sock, zmq.POLLIN)
Example #3
Source File: zmqwrapper.py From BAG_framework with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, port, pipeline=100, host='localhost', log_file=None): """Create a new ZMQDealer object. """ context = zmq.Context.instance() # noinspection PyUnresolvedReferences self.socket = context.socket(zmq.DEALER) self.socket.hwm = pipeline self.socket.connect('tcp://%s:%d' % (host, port)) self._log_file = log_file self.poller = zmq.Poller() # noinspection PyUnresolvedReferences self.poller.register(self.socket, zmq.POLLIN) if self._log_file is not None: self._log_file = Path(self._log_file).resolve() # If log file directory does not exists, create it log_dir: Path = self._log_file.parent log_dir.mkdir(parents=True, exist_ok=True) # time stamp the file now = datetime.now() time_stamp = now.strftime('%Y%m%d_%H%M%S%f') ext = self._log_file.suffix self._log_file = str(log_dir / f'{self._log_file.stem}_{time_stamp}{ext}')
Example #4
Source File: mainloop.py From supvisors with Apache License 2.0 | 6 votes |
def run(self): """ Contents of the infinite loop. """ # Create zmq sockets sockets = SupvisorsZmq(self.supvisors) # create poller poller = zmq.Poller() # register sockets poller.register(sockets.internal_subscriber.socket, zmq.POLLIN) poller.register(sockets.puller.socket, zmq.POLLIN) # poll events forever while not self.stopping(): socks = dict(poller.poll(500)) # test stop condition again: if Supervisor is stopping, # any XML-RPC call would block this thread, and the other # because of the join if not self.stopping(): self.check_requests(sockets, socks) self.check_events(sockets.internal_subscriber, socks) # close resources gracefully poller.unregister(sockets.puller.socket) poller.unregister(sockets.internal_subscriber.socket) sockets.close()
Example #5
Source File: mainloop.py From supvisors with Apache License 2.0 | 6 votes |
def check_events(self, subscriber, socks): """ Forward external Supervisor events to main thread. """ if subscriber.socket in socks and \ socks[subscriber.socket] == zmq.POLLIN: try: message = subscriber.receive() except: print >> stderr, '[ERROR] failed to get data from subscriber' else: # The events received are not processed directly in this thread # because it would conflict with the processing in the # Supervisor thread, as they use the same data. # That's why a RemoteCommunicationEvent is used to push the # event in the Supervisor thread. self.send_remote_comm_event( RemoteCommEvents.SUPVISORS_EVENT, json.dumps(message))
Example #6
Source File: _test_asyncio.py From vnpy_crypto with MIT License | 6 votes |
def test_poll_base_socket(self): @asyncio.coroutine def test(): ctx = zmq.Context() url = 'inproc://test' a = ctx.socket(zmq.PUSH) b = ctx.socket(zmq.PULL) self.sockets.extend([a, b]) a.bind(url) b.connect(url) poller = zaio.Poller() poller.register(b, zmq.POLLIN) f = poller.poll(timeout=1000) assert not f.done() a.send_multipart([b'hi', b'there']) evt = yield from f self.assertEqual(evt, [(b, zmq.POLLIN)]) recvd = b.recv_multipart() self.assertEqual(recvd, [b'hi', b'there']) self.loop.run_until_complete(test())
Example #7
Source File: _test_asyncio.py From vnpy_crypto with MIT License | 6 votes |
def test_poll(self): @asyncio.coroutine def test(): a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL) f = b.poll(timeout=0) yield from asyncio.sleep(0) self.assertEqual(f.result(), 0) f = b.poll(timeout=1) assert not f.done() evt = yield from f self.assertEqual(evt, 0) f = b.poll(timeout=1000) assert not f.done() yield from a.send_multipart([b'hi', b'there']) evt = yield from f self.assertEqual(evt, zmq.POLLIN) recvd = yield from b.recv_multipart() self.assertEqual(recvd, [b'hi', b'there']) self.loop.run_until_complete(test())
Example #8
Source File: eventloops.py From Computable with MIT License | 6 votes |
def _notify_stream_qt(kernel, stream): from IPython.external.qt_for_kernel import QtCore if _on_os_x_10_9() and kernel._darwin_app_nap: from IPython.external.appnope import nope_scope as context else: from IPython.core.interactiveshell import NoOpContext as context def process_stream_events(): while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN: with context(): kernel.do_one_iteration() fd = stream.getsockopt(zmq.FD) notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app) notifier.activated.connect(process_stream_events)
Example #9
Source File: ipkernel.py From Computable with MIT License | 6 votes |
def _abort_queue(self, stream): poller = zmq.Poller() poller.register(stream.socket, zmq.POLLIN) while True: idents,msg = self.session.recv(stream, zmq.NOBLOCK, content=True) if msg is None: return self.log.info("Aborting:") self.log.info("%s", msg) msg_type = msg['header']['msg_type'] reply_type = msg_type.split('_')[0] + '_reply' status = {'status' : 'aborted'} md = {'engine' : self.ident} md.update(status) reply_msg = self.session.send(stream, reply_type, metadata=md, content=status, parent=msg, ident=idents) self.log.debug("%s", reply_msg) # We need to wait a bit for requests to come in. This can probably # be set shorter for true asynchronous clients. poller.poll(50)
Example #10
Source File: client_api.py From GaragePi with MIT License | 6 votes |
def __send_recv_msg(self, msg): # Make sure we're sending bytes instead of strings msg = list(map(lambda s: str.encode(s) if type(s) is str else s, msg)) self.__socket.send_multipart(msg) events = dict(self.__poller.poll(SEND_TIMEOUT)) if events.get(self.__socket) == zmq.POLLIN: try: # Receive response and make sure the return is converted to string if necessary ret_msg = self.__socket.recv_multipart()[0] return bytes.decode(ret_msg) if type(ret_msg) is bytes else ret_msg except zmq.error.Again: # If the receive timed out then return None self.__logger.warning("Receive operation timed out!") self.__create_socket() return None else: self.__logger.warning("Send operation timed out!") self.__create_socket() return None
Example #11
Source File: __init__.py From stytra with GNU General Public License v3.0 | 6 votes |
def check_trigger(self): """ Wait to receive the json file and reply with the duration of the experiment. Then, to the `queue_trigger_params` the received dict, so that the `Experiment` can store it with the rest of the data. """ poller = zmq.Poller() poller.register(self.zmq_socket, zmq.POLLIN) try: self.protocol_duration = self.duration_queue.get(timeout=0.0001) print(self.protocol_duration) except Empty: pass if poller.poll(10): self.scope_config = self.zmq_socket.recv_json() self.device_params_queue.put(self.scope_config) self.zmq_socket.send_json(self.protocol_duration) return True else: return False
Example #12
Source File: thread.py From pySINDy with MIT License | 6 votes |
def run(self): """Start the Authentication Agent thread task""" self.authenticator.start() self.started.set() zap = self.authenticator.zap_socket poller = zmq.Poller() poller.register(self.pipe, zmq.POLLIN) poller.register(zap, zmq.POLLIN) while True: try: socks = dict(poller.poll()) except zmq.ZMQError: break # interrupted if self.pipe in socks and socks[self.pipe] == zmq.POLLIN: terminate = self._handle_pipe() if terminate: break if zap in socks and socks[zap] == zmq.POLLIN: self._handle_zap() self.pipe.close() self.authenticator.stop()
Example #13
Source File: zmqstream.py From vnpy_crypto with MIT License | 6 votes |
def _handle_events(self, fd, events): """This method is the actual handler for IOLoop, that gets called whenever an event on my socket is posted. It dispatches to _handle_recv, etc.""" if not self.socket: gen_log.warning("Got events for closed stream %s", fd) return zmq_events = self.socket.EVENTS try: # dispatch events: if zmq_events & zmq.POLLIN and self.receiving(): self._handle_recv() if not self.socket: return if zmq_events & zmq.POLLOUT and self.sending(): self._handle_send() if not self.socket: return # rebuild the poll state self._rebuild_io_state() except Exception: gen_log.error("Uncaught exception in zmqstream callback", exc_info=True) raise
Example #14
Source File: listeners.py From pymeasure with MIT License | 6 votes |
def __init__(self, port, topic='', timeout=0.01): """ Constructs the Listener object with a subscriber port over which to listen for messages :param port: TCP port to listen on :param topic: Topic to listen on :param timeout: Timeout in seconds to recheck stop flag """ super().__init__() self.port = port self.topic = topic self.context = zmq.Context() log.debug("%s has ZMQ Context: %r" % (self.__class__.__name__, self.context)) self.subscriber = self.context.socket(zmq.SUB) self.subscriber.connect('tcp://localhost:%d' % port) self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode()) log.info("%s connected to '%s' topic on tcp://localhost:%d" % ( self.__class__.__name__, topic, port)) self.poller = zmq.Poller() self.poller.register(self.subscriber, zmq.POLLIN) self.timeout = timeout
Example #15
Source File: zmqstream.py From pySINDy with MIT License | 6 votes |
def _handle_events(self, fd, events): """This method is the actual handler for IOLoop, that gets called whenever an event on my socket is posted. It dispatches to _handle_recv, etc.""" if not self.socket: gen_log.warning("Got events for closed stream %s", fd) return zmq_events = self.socket.EVENTS try: # dispatch events: if zmq_events & zmq.POLLIN and self.receiving(): self._handle_recv() if not self.socket: return if zmq_events & zmq.POLLOUT and self.sending(): self._handle_send() if not self.socket: return # rebuild the poll state self._rebuild_io_state() except Exception: gen_log.error("Uncaught exception in zmqstream callback", exc_info=True) raise
Example #16
Source File: thread.py From vnpy_crypto with MIT License | 6 votes |
def run(self): """Start the Authentication Agent thread task""" self.authenticator.start() self.started.set() zap = self.authenticator.zap_socket poller = zmq.Poller() poller.register(self.pipe, zmq.POLLIN) poller.register(zap, zmq.POLLIN) while True: try: socks = dict(poller.poll()) except zmq.ZMQError: break # interrupted if self.pipe in socks and socks[self.pipe] == zmq.POLLIN: terminate = self._handle_pipe() if terminate: break if zap in socks and socks[zap] == zmq.POLLIN: self._handle_zap() self.pipe.close() self.authenticator.stop()
Example #17
Source File: listeners.py From pymeasure with MIT License | 6 votes |
def __init__(self, port, topic='', timeout=0.01): """ Constructs the Listener object with a subscriber port over which to listen for messages :param port: TCP port to listen on :param topic: Topic to listen on :param timeout: Timeout in seconds to recheck stop flag """ super().__init__() self.port = port self.topic = topic self.context = zmq.Context() log.debug("%s has ZMQ Context: %r" % (self.__class__.__name__, self.context)) self.subscriber = self.context.socket(zmq.SUB) self.subscriber.connect('tcp://localhost:%d' % port) self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode()) log.info("%s connected to '%s' topic on tcp://localhost:%d" % ( self.__class__.__name__, topic, port)) self.poller = zmq.Poller() self.poller.register(self.subscriber, zmq.POLLIN) self.timeout = timeout
Example #18
Source File: test_future.py From vnpy_crypto with MIT License | 6 votes |
def test_poll_base_socket(self): @gen.coroutine def test(): ctx = zmq.Context() url = 'inproc://test' a = ctx.socket(zmq.PUSH) b = ctx.socket(zmq.PULL) self.sockets.extend([a, b]) a.bind(url) b.connect(url) poller = future.Poller() poller.register(b, zmq.POLLIN) f = poller.poll(timeout=1000) assert not f.done() a.send_multipart([b'hi', b'there']) evt = yield f self.assertEqual(evt, [(b, zmq.POLLIN)]) recvd = b.recv_multipart() self.assertEqual(recvd, [b'hi', b'there']) a.close() b.close() ctx.term() self.loop.run_sync(test)
Example #19
Source File: zmq_worker.py From funcX with Apache License 2.0 | 6 votes |
def reconnect_to_broker(self): """Connect or reconnect to broker""" if self.worker: self.poller.unregister(self.worker) self.worker.close() self.worker = self.ctx.socket(zmq.DEALER) self.worker.linger = 0 self.worker.connect(self.broker) self.poller.register(self.worker, zmq.POLLIN) if self.verbose: logging.info("I: connecting to broker at %s…", self.broker) # Register service with broker self.send_to_broker(MDP.W_READY, pickle.dumps(self.service), []) # If liveness hits zero, queue is considered disconnected self.liveness = self.HEARTBEAT_LIVENESS self.heartbeat_at = time.time() + 1e-3 * self.heartbeat
Example #20
Source File: test_messenger.py From networkzero with MIT License | 6 votes |
def support_test_send_to_multiple_addresses(self, address1, address2): poller = zmq.Poller() socket1 = self.context.socket(roles['listener']) socket2 = self.context.socket(roles['listener']) try: socket1.bind("tcp://%s" % address1) socket2.bind("tcp://%s" % address2) poller.register(socket1, zmq.POLLIN) poller.register(socket2, zmq.POLLIN) polled = dict(poller.poll(2000)) if socket1 in polled: socket1.recv() socket1.send(nw0.sockets._serialise(address1)) elif socket2 in polled: socket2.recv() socket2.send(nw0.sockets._serialise(address2)) else: raise RuntimeError("Nothing found") finally: socket1.close() socket2.close()
Example #21
Source File: _future.py From pySINDy with MIT License | 5 votes |
def _add_recv_event(self, kind, kwargs=None, future=None): """Add a recv event, returning the corresponding Future""" f = future or self._Future() if kind.startswith('recv') and kwargs.get('flags', 0) & _zmq.DONTWAIT: # short-circuit non-blocking calls recv = getattr(self._shadow_sock, kind) try: r = recv(**kwargs) except Exception as e: f.set_exception(e) else: f.set_result(r) return f # we add it to the list of futures before we add the timeout as the # timeout will remove the future from recv_futures to avoid leaks self._recv_futures.append( _FutureEvent(f, kind, kwargs, msg=None) ) # Don't let the Future sit in _recv_events after it's done f.add_done_callback(lambda f: self._remove_finished_future(f, self._recv_futures)) if hasattr(_zmq, 'RCVTIMEO'): timeout_ms = self._shadow_sock.rcvtimeo if timeout_ms >= 0: self._add_timeout(f, timeout_ms * 1e-3) if self._shadow_sock.EVENTS & POLLIN: # recv immediately, if we can self._handle_recv() if self._recv_futures: self._add_io_state(POLLIN) return f
Example #22
Source File: zmqstream.py From pySINDy with MIT License | 5 votes |
def _rebuild_io_state(self): """rebuild io state based on self.sending() and receiving()""" if self.socket is None: return state = 0 if self.receiving(): state |= zmq.POLLIN if self.sending(): state |= zmq.POLLOUT self._state = state self._update_handler(state)
Example #23
Source File: core.py From Computable with MIT License | 5 votes |
def _wait_read(self): assert self.__readable.ready(), "Only one greenlet can be waiting on this event" self.__readable = AsyncResult() # timeout is because libzmq cannot always be trusted to play nice with libevent. # I can only confirm that this actually happens for send, but lets be symmetrical # with our dirty hacks. # this is effectively a maximum poll interval of 1s tic = time.time() dt = self._gevent_bug_timeout if dt: timeout = gevent.Timeout(seconds=dt) else: timeout = None try: if timeout: timeout.start() self.__readable.get(block=True) except gevent.Timeout as t: if t is not timeout: raise toc = time.time() # gevent bug: get can raise timeout even on clean return # don't display zmq bug warning for gevent bug (this is getting ridiculous) if self._debug_gevent and timeout and toc-tic > dt and \ self.getsockopt(zmq.EVENTS) & zmq.POLLIN: print("BUG: gevent may have missed a libzmq recv event on %i!" % self.FD, file=sys.stderr) finally: if timeout: timeout.cancel() self.__readable.set()
Example #24
Source File: channels.py From Computable with MIT License | 5 votes |
def _create_socket(self): if self.socket is not None: # close previous socket, before opening a new one self.poller.unregister(self.socket) self.socket.close() self.socket = self.context.socket(zmq.REQ) self.socket.setsockopt(zmq.LINGER, 0) self.socket.connect(self.address) self.poller.register(self.socket, zmq.POLLIN)
Example #25
Source File: ipkernel.py From Computable with MIT License | 5 votes |
def do_one_iteration(self): """step eventloop just once""" InteractiveShell.set_instance(self.shell) if self.control_stream: self.control_stream.flush() for stream in self.shell_streams: # handle at most one request per iteration stream.flush(zmq.POLLIN, 1) stream.flush(zmq.POLLOUT)
Example #26
Source File: iostream.py From Computable with MIT License | 5 votes |
def _setup_pipe_in(self): """setup listening pipe for subprocesses""" ctx = self.pub_socket.context # use UUID to authenticate pipe messages self._pipe_uuid = uuid.uuid4().bytes self._pipe_in = ctx.socket(zmq.PULL) self._pipe_in.linger = 0 self._pipe_port = self._pipe_in.bind_to_random_port("tcp://127.0.0.1") self._pipe_poller = zmq.Poller() self._pipe_poller.register(self._pipe_in, zmq.POLLIN)
Example #27
Source File: monitoredqueue.py From Computable with MIT License | 5 votes |
def monitored_queue(in_socket, out_socket, mon_socket, in_prefix=b'in', out_prefix=b'out'): swap_ids = in_socket.type == zmq.ROUTER and out_socket.type == zmq.ROUTER poller = zmq.Poller() poller.register(in_socket, zmq.POLLIN) poller.register(out_socket, zmq.POLLIN) while True: events = dict(poller.poll()) if in_socket in events: _relay(in_socket, out_socket, mon_socket, in_prefix, swap_ids) if out_socket in events: _relay(out_socket, in_socket, mon_socket, out_prefix, swap_ids)
Example #28
Source File: poll.py From Computable with MIT License | 5 votes |
def _get_descriptors(self): """Returns three elements tuple with socket descriptors ready for gevent.select.select """ rlist = [] wlist = [] xlist = [] for socket, flags in self.sockets: if isinstance(socket, zmq.Socket): rlist.append(socket.getsockopt(zmq.FD)) continue elif isinstance(socket, int): fd = socket elif hasattr(socket, 'fileno'): try: fd = int(socket.fileno()) except: raise ValueError('fileno() must return an valid integer fd') else: raise TypeError('Socket must be a 0MQ socket, an integer fd ' 'or have a fileno() method: %r' % socket) if flags & zmq.POLLIN: rlist.append(fd) if flags & zmq.POLLOUT: wlist.append(fd) if flags & zmq.POLLERR: xlist.append(fd) return (rlist, wlist, xlist)
Example #29
Source File: device.py From Computable with MIT License | 5 votes |
def device(device_type, isocket, osocket): """Start a zeromq device (gevent-compatible). Unlike the true zmq.device, this does not release the GIL. Parameters ---------- device_type : (QUEUE, FORWARDER, STREAMER) The type of device to start (ignored). isocket : Socket The Socket instance for the incoming traffic. osocket : Socket The Socket instance for the outbound traffic. """ p = Poller() if osocket == -1: osocket = isocket p.register(isocket, zmq.POLLIN) p.register(osocket, zmq.POLLIN) while True: events = dict(p.poll()) if isocket in events: osocket.send_multipart(isocket.recv_multipart()) if osocket in events: isocket.send_multipart(osocket.recv_multipart())
Example #30
Source File: core.py From Computable with MIT License | 5 votes |
def __state_changed(self, event=None, _evtype=None): if self.closed: self.__cleanup_events() return try: # avoid triggering __state_changed from inside __state_changed events = super(_Socket, self).getsockopt(zmq.EVENTS) except zmq.ZMQError as exc: self.__writable.set_exception(exc) self.__readable.set_exception(exc) else: if events & zmq.POLLOUT: self.__writable.set() if events & zmq.POLLIN: self.__readable.set()