Python zmq.Poller() Examples
The following are 30
code examples of zmq.Poller().
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_socket.py From pySINDy with MIT License | 6 votes |
def test_subscribe_method(self): pub, sub = self.create_bound_pair(zmq.PUB, zmq.SUB) sub.subscribe('prefix') sub.subscribe = 'c' p = zmq.Poller() p.register(sub, zmq.POLLIN) # wait for subscription handshake for i in range(100): pub.send(b'canary') events = p.poll(250) if events: break self.recv(sub) pub.send(b'prefixmessage') msg = self.recv(sub) self.assertEqual(msg, b'prefixmessage') sub.unsubscribe('prefix') pub.send(b'prefixmessage') events = p.poll(1000) self.assertEqual(events, []) # Travis can't handle how much memory PyPy uses on this test
Example #2
Source File: interchange.py From parsl with Apache License 2.0 | 6 votes |
def dealer_interchange(manager_ip="localhost", manager_port=5559, worker_port=5560): context = zmq.Context() incoming = context.socket(zmq.ROUTER) outgoing = context.socket(zmq.DEALER) incoming.connect("tcp://{}:{}".format(manager_ip, manager_port)) outgoing.bind("tcp://*:{}".format(worker_port)) poller = zmq.Poller() poller.register(incoming, zmq.POLLIN) poller.register(outgoing, zmq.POLLIN) while True: socks = dict(poller.poll(1)) if socks.get(incoming) == zmq.POLLIN: message = incoming.recv_multipart() logger.debug("[interchange] New task {}".format(message)) outgoing.send_multipart(message) if socks.get(outgoing) == zmq.POLLIN: message = outgoing.recv_multipart() logger.debug("[interchange] New result {}".format(message)) incoming.send_multipart(message)
Example #3
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 #4
Source File: test_socket.py From vnpy_crypto with MIT License | 6 votes |
def test_subscribe_method(self): pub, sub = self.create_bound_pair(zmq.PUB, zmq.SUB) sub.subscribe('prefix') sub.subscribe = 'c' p = zmq.Poller() p.register(sub, zmq.POLLIN) # wait for subscription handshake for i in range(100): pub.send(b'canary') events = p.poll(250) if events: break self.recv(sub) pub.send(b'prefixmessage') msg = self.recv(sub) self.assertEqual(msg, b'prefixmessage') sub.unsubscribe('prefix') pub.send(b'prefixmessage') events = p.poll(1000) self.assertEqual(events, []) # Travis can't handle how much memory PyPy uses on this test
Example #5
Source File: client.py From bluesky with GNU General Public License v3.0 | 6 votes |
def __init__(self, actnode_topics=b''): ctx = zmq.Context.instance() self.event_io = ctx.socket(zmq.DEALER) self.stream_in = ctx.socket(zmq.SUB) self.poller = zmq.Poller() self.host_id = b'' self.client_id = b'\x00' + os.urandom(4) self.sender_id = b'' self.servers = dict() self.act = b'' self.actroute = [] self.acttopics = actnode_topics self.discovery = None # Signals self.nodes_changed = Signal() self.server_discovered = Signal() self.signal_quit = Signal() self.event_received = Signal() self.stream_received = Signal() # Tell bluesky that this client will manage the network I/O bluesky.net = self
Example #6
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 #7
Source File: zmq_pipes.py From funcX with Apache License 2.0 | 6 votes |
def __init__(self, ip_address, port_range): """ Parameters ---------- ip_address: str IP address of the client (where Parsl runs) port_range: tuple(int, int) Port range for the comms between client and interchange """ self.context = zmq.Context() self.zmq_socket = self.context.socket(zmq.DEALER) self.zmq_socket.set_hwm(0) self.port = self.zmq_socket.bind_to_random_port("tcp://{}".format(ip_address), min_port=port_range[0], max_port=port_range[1]) self.poller = zmq.Poller() self.poller.register(self.zmq_socket, zmq.POLLOUT)
Example #8
Source File: DWX_ZeroMQ_Connector_v2_0_1_RC8.py From dwx-zeromq-connector with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _DWX_ZMQ_SHUTDOWN_(self): # Set INACTIVE self._ACTIVE = False # Get all threads to shutdown if self._MarketData_Thread is not None: self._MarketData_Thread.join() if self._PUSH_Monitor_Thread is not None: self._PUSH_Monitor_Thread.join() if self._PULL_Monitor_Thread is not None: self._PULL_Monitor_Thread.join() # Unregister sockets from Poller self._poller.unregister(self._PULL_SOCKET) self._poller.unregister(self._SUB_SOCKET) print("\n++ [KERNEL] Sockets unregistered from ZMQ Poller()! ++") # Terminate context self._ZMQ_CONTEXT.destroy(0) print("\n++ [KERNEL] ZeroMQ Context Terminated.. shut down safely complete! :)") ##########################################################################
Example #9
Source File: p2p.py From gateway with GNU Affero General Public License v3.0 | 6 votes |
def _send_raw(self, serialized): self.create_socket() self._socket.send(serialized, zmq.NOBLOCK) poller = zmq.Poller() poller.register(self._socket, zmq.POLLIN) if poller.poll(self._timeout * 1000): msg = self._socket.recv() self.on_message(msg) self.cleanup_socket() else: self._transport.log("Peer " + self._address + " timed out.") self.cleanup_socket() self._transport.remove_peer(self._address)
Example #10
Source File: Helper.py From AIL-framework with GNU Affero General Public License v3.0 | 6 votes |
def subscribe(self): if self.redis_sub: for msg in self.subscribers.listen(): if msg.get('data', None) is not None: yield msg['data'] elif self.zmq_sub: # Initialize poll set poller = zmq.Poller() for subscriber in self.subscribers: poller.register(subscriber, zmq.POLLIN) while True: socks = dict(poller.poll()) for subscriber in self.subscribers: if subscriber in socks: message = subscriber.recv() yield message.split(b' ', 1)[1] else: raise Exception('No subscribe function defined')
Example #11
Source File: zmqstream.py From Computable with MIT License | 6 votes |
def __init__(self, socket, io_loop=None): self.socket = socket self.io_loop = io_loop or IOLoop.instance() self.poller = zmq.Poller() self._send_queue = Queue() self._recv_callback = None self._send_callback = None self._close_callback = None self._recv_copy = False self._flushed = False self._state = self.io_loop.ERROR self._init_io_state() # shortcircuit some socket methods self.bind = self.socket.bind self.bind_to_random_port = self.socket.bind_to_random_port self.connect = self.socket.connect self.setsockopt = self.socket.setsockopt self.getsockopt = self.socket.getsockopt self.setsockopt_string = self.socket.setsockopt_string self.getsockopt_string = self.socket.getsockopt_string self.setsockopt_unicode = self.socket.setsockopt_unicode self.getsockopt_unicode = self.socket.getsockopt_unicode
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
Source File: test_poll.py From pySINDy 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 #18
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 #19
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 #20
Source File: IVVIDIG_main_eth.py From qkit with GNU General Public License v2.0 | 6 votes |
def _open_zmq_connection(self): ''' Connects to the raspberry via zmq/tcp Input: None Output: None ''' self.context = zmq.Context(1) print("Connecting to Raspberry Pi") self.client = self.context.socket(zmq.REQ) self.client.connect("tcp://%s:%s"%(self._address,self._port)) # raspi address self.poll = zmq.Poller() self.poll.register(self.client, zmq.POLLIN)
Example #21
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 #22
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 #23
Source File: test_poll.py From pySINDy with MIT License | 5 votes |
def test_pair(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) # Sleep to allow sockets to connect. wait() poller = self.Poller() poller.register(s1, zmq.POLLIN|zmq.POLLOUT) poller.register(s2, zmq.POLLIN|zmq.POLLOUT) # Poll result should contain both sockets socks = dict(poller.poll()) # Now make sure that both are send ready. self.assertEqual(socks[s1], zmq.POLLOUT) self.assertEqual(socks[s2], zmq.POLLOUT) # Now do a send on both, wait and test for zmq.POLLOUT|zmq.POLLIN s1.send(b'msg1') s2.send(b'msg2') wait() socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT|zmq.POLLIN) self.assertEqual(socks[s2], zmq.POLLOUT|zmq.POLLIN) # Make sure that both are in POLLOUT after recv. s1.recv() s2.recv() socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT) self.assertEqual(socks[s2], zmq.POLLOUT) poller.unregister(s1) poller.unregister(s2)
Example #24
Source File: test_poll.py From pySINDy with MIT License | 5 votes |
def test_pubsub(self): s1, s2 = self.create_bound_pair(zmq.PUB, zmq.SUB) s2.setsockopt(zmq.SUBSCRIBE, b'') # Sleep to allow sockets to connect. wait() poller = self.Poller() poller.register(s1, zmq.POLLIN|zmq.POLLOUT) poller.register(s2, zmq.POLLIN) # Now make sure that both are send ready. socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT) self.assertEqual(s2 in socks, 0) # Make sure that s1 stays in POLLOUT after a send. s1.send(b'msg1') socks = dict(poller.poll()) self.assertEqual(socks[s1], zmq.POLLOUT) # Make sure that s2 is POLLIN after waiting. wait() socks = dict(poller.poll()) self.assertEqual(socks[s2], zmq.POLLIN) # Make sure that s2 goes into 0 after recv. s2.recv() socks = dict(poller.poll()) self.assertEqual(s2 in socks, 0) poller.unregister(s1) poller.unregister(s2)
Example #25
Source File: paramclient.py From hfsoftmax with MIT License | 5 votes |
def __init__(self, _id): print('Connecting to ParamServer ...') context = zmq.Context() socket = context.socket(zmq.DEALER) identity = u'%d' % _id socket.identity = identity.encode('ascii') socket.connect('tcp://localhost:5570') print('Client %s started' % (identity)) self._poll = zmq.Poller() self._poll.register(socket, zmq.POLLIN) self._socket = socket self._context = context self._register()
Example #26
Source File: test_poll.py From pySINDy with MIT License | 5 votes |
def test_raw(self): r, w = os.pipe() r = os.fdopen(r, 'rb') w = os.fdopen(w, 'wb') p = self.Poller() p.register(r, zmq.POLLIN) socks = dict(p.poll(1)) assert socks == {} w.write(b'x') w.flush() socks = dict(p.poll(1)) assert socks == {r.fileno(): zmq.POLLIN} w.close() r.close()
Example #27
Source File: test_retry_eintr.py From pySINDy with MIT License | 5 votes |
def test_retry_poll(self): x, y = self.create_bound_pair() poller = zmq.Poller() poller.register(x, zmq.POLLIN) self.alarm() def send(): time.sleep(2 * self.signal_delay) y.send(b('ping')) t = Thread(target=send) t.start() evts = dict(poller.poll(2 * self.timeout_ms)) t.join() assert x in evts assert self.timer_fired x.recv()
Example #28
Source File: telemetry.py From autodrome with MIT License | 5 votes |
def __init__(self, address: str=Message.Bind.address): self.address = address ctx = zmq.Context() self.socket = ctx.socket(zmq.REQ) self.socket.connect(Telemetry.Message.Bind.address) self.poller = zmq.Poller() self.poller.register(self.socket, flags=zmq.POLLIN) request = self.Request.new_message() request_bytes = request.to_bytes() self.socket.send(request_bytes)
Example #29
Source File: zmq_pipes.py From parsl with Apache License 2.0 | 5 votes |
def __init__(self, ip_address, port_range): """ TODO: docstring """ self.context = zmq.Context() self.zmq_socket = self.context.socket(zmq.DEALER) self.zmq_socket.set_hwm(0) self.port = self.zmq_socket.bind_to_random_port("tcp://{}".format(ip_address), min_port=port_range[0], max_port=port_range[1]) self.poller = zmq.Poller() self.poller.register(self.zmq_socket, zmq.POLLOUT)
Example #30
Source File: test_poll.py From pySINDy with MIT License | 5 votes |
def test_wakeup(self): s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR) poller = self.Poller() poller.register(s2, zmq.POLLIN) tic = time.time() r = gevent.spawn(lambda: poller.poll(10000)) s = gevent.spawn(lambda: s1.send(b'msg1')) r.join() toc = time.time() self.assertTrue(toc-tic < 1)