Python zmq.SUBSCRIBE Examples
The following are 30
code examples of zmq.SUBSCRIBE().
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: scoopzmq.py From scoop with GNU Lesser General Public License v3.0 | 6 votes |
def _addBroker(self, brokerEntry): # Add a broker to the socket and the infosocket. broker_address = "tcp://{hostname}:{port}".format( hostname=brokerEntry.hostname, port=brokerEntry.task_port, ) meta_address = "tcp://{hostname}:{port}".format( hostname=brokerEntry.hostname, port=brokerEntry.info_port, ) self.socket.connect(broker_address) self.infoSocket.connect(meta_address) self.infoSocket.setsockopt(zmq.SUBSCRIBE, b"") self.broker_set.add(brokerEntry)
Example #2
Source File: test_socket.py From vnpy_crypto with MIT License | 6 votes |
def test_unicode_sockopts(self): """test setting/getting sockopts with unicode strings""" topic = "tést" if str is not unicode: topic = topic.decode('utf8') p,s = self.create_bound_pair(zmq.PUB, zmq.SUB) self.assertEqual(s.send_unicode, s.send_unicode) self.assertEqual(p.recv_unicode, p.recv_unicode) self.assertRaises(TypeError, s.setsockopt, zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.setsockopt, zmq.IDENTITY, topic) s.setsockopt_unicode(zmq.IDENTITY, topic, 'utf16') self.assertRaises(TypeError, s.setsockopt, zmq.AFFINITY, topic) s.setsockopt_unicode(zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.getsockopt_unicode, zmq.AFFINITY) self.assertRaisesErrno(zmq.EINVAL, s.getsockopt_unicode, zmq.SUBSCRIBE) identb = s.getsockopt(zmq.IDENTITY) identu = identb.decode('utf16') identu2 = s.getsockopt_unicode(zmq.IDENTITY, 'utf16') self.assertEqual(identu, identu2) time.sleep(0.1) # wait for connection/subscription p.send_unicode(topic,zmq.SNDMORE) p.send_unicode(topic*2, encoding='latin-1') self.assertEqual(topic, s.recv_unicode()) self.assertEqual(topic*2, s.recv_unicode(encoding='latin-1'))
Example #3
Source File: test_log.py From pySINDy with MIT License | 6 votes |
def test_root_topic(self): logger, handler, sub = self.connect_handler() handler.socket.bind(self.iface) sub2 = sub.context.socket(zmq.SUB) self.sockets.append(sub2) sub2.connect(self.iface) sub2.setsockopt(zmq.SUBSCRIBE, b'') handler.root_topic = b'twoonly' msg1 = 'ignored' logger.info(msg1) self.assertRaisesErrno(zmq.EAGAIN, sub.recv, zmq.NOBLOCK) topic,msg2 = sub2.recv_multipart() self.assertEqual(topic, b'twoonly.INFO') self.assertEqual(msg2, b(msg1)+b'\n') logger.removeHandler(handler)
Example #4
Source File: test_monqueue.py From vnpy_crypto with MIT License | 6 votes |
def build_device(self, mon_sub=b"", in_prefix=b'in', out_prefix=b'out'): self.device = devices.ThreadMonitoredQueue(zmq.PAIR, zmq.PAIR, zmq.PUB, in_prefix, out_prefix) alice = self.context.socket(zmq.PAIR) bob = self.context.socket(zmq.PAIR) mon = self.context.socket(zmq.SUB) aport = alice.bind_to_random_port('tcp://127.0.0.1') bport = bob.bind_to_random_port('tcp://127.0.0.1') mport = mon.bind_to_random_port('tcp://127.0.0.1') mon.setsockopt(zmq.SUBSCRIBE, mon_sub) self.device.connect_in("tcp://127.0.0.1:%i"%aport) self.device.connect_out("tcp://127.0.0.1:%i"%bport) self.device.connect_mon("tcp://127.0.0.1:%i"%mport) self.device.start() time.sleep(.2) try: # this is currenlty necessary to ensure no dropped monitor messages # see LIBZMQ-248 for more info mon.recv_multipart(zmq.NOBLOCK) except zmq.ZMQError: pass self.sockets.extend([alice, bob, mon]) return alice, bob, mon
Example #5
Source File: banyan_base_multi.py From python_banyan with GNU Affero General Public License v3.0 | 6 votes |
def set_subscriber_topic(self, topic, subscriber_socket): """ This method sets a subscriber topic. You can subscribe to multiple topics by calling this method for each topic. :param topic: A topic string :param subscriber_socket: subscriber socket :return: """ # make sure topic is a string if not type(topic) is str: raise TypeError('Subscriber topic must be python_banyan string') # does the subscriber socket exist? if subscriber_socket: subscriber_socket.setsockopt(zmq.SUBSCRIBE, topic.encode()) else: raise ValueError('set_subscriber_topic: socket is None')
Example #6
Source File: test_log.py From pySINDy with MIT License | 6 votes |
def test_init_socket(self): pub,sub = self.create_bound_pair(zmq.PUB, zmq.SUB) logger = self.logger handler = handlers.PUBHandler(pub) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) self.assertTrue(handler.socket is pub) self.assertTrue(handler.ctx is pub.context) self.assertTrue(handler.ctx is self.context) sub.setsockopt(zmq.SUBSCRIBE, b(self.topic)) import time; time.sleep(0.1) msg1 = 'message' logger.info(msg1) (topic, msg2) = sub.recv_multipart() self.assertEqual(topic, b'zmq.INFO') self.assertEqual(msg2, b(msg1)+b'\n') logger.removeHandler(handler)
Example #7
Source File: test_log.py From vnpy_crypto with MIT License | 6 votes |
def test_init_iface(self): logger = self.logger ctx = self.context handler = handlers.PUBHandler(self.iface) self.assertFalse(handler.ctx is ctx) self.sockets.append(handler.socket) # handler.ctx.term() handler = handlers.PUBHandler(self.iface, self.context) self.sockets.append(handler.socket) self.assertTrue(handler.ctx is ctx) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) sub = ctx.socket(zmq.SUB) self.sockets.append(sub) sub.setsockopt(zmq.SUBSCRIBE, b(self.topic)) sub.connect(self.iface) import time; time.sleep(0.25) msg1 = 'message' logger.info(msg1) (topic, msg2) = sub.recv_multipart() self.assertEqual(topic, b'zmq.INFO') self.assertEqual(msg2, b(msg1)+b'\n') logger.removeHandler(handler)
Example #8
Source File: test_log.py From vnpy_crypto with MIT License | 6 votes |
def test_init_socket(self): pub,sub = self.create_bound_pair(zmq.PUB, zmq.SUB) logger = self.logger handler = handlers.PUBHandler(pub) handler.setLevel(logging.DEBUG) handler.root_topic = self.topic logger.addHandler(handler) self.assertTrue(handler.socket is pub) self.assertTrue(handler.ctx is pub.context) self.assertTrue(handler.ctx is self.context) sub.setsockopt(zmq.SUBSCRIBE, b(self.topic)) import time; time.sleep(0.1) msg1 = 'message' logger.info(msg1) (topic, msg2) = sub.recv_multipart() self.assertEqual(topic, b'zmq.INFO') self.assertEqual(msg2, b(msg1)+b'\n') logger.removeHandler(handler)
Example #9
Source File: test_log.py From vnpy_crypto with MIT License | 6 votes |
def test_root_topic(self): logger, handler, sub = self.connect_handler() handler.socket.bind(self.iface) sub2 = sub.context.socket(zmq.SUB) self.sockets.append(sub2) sub2.connect(self.iface) sub2.setsockopt(zmq.SUBSCRIBE, b'') handler.root_topic = b'twoonly' msg1 = 'ignored' logger.info(msg1) self.assertRaisesErrno(zmq.EAGAIN, sub.recv, zmq.NOBLOCK) topic,msg2 = sub2.recv_multipart() self.assertEqual(topic, b'twoonly.INFO') self.assertEqual(msg2, b(msg1)+b'\n') logger.removeHandler(handler)
Example #10
Source File: test_socket.py From pySINDy with MIT License | 6 votes |
def test_unicode_sockopts(self): """test setting/getting sockopts with unicode strings""" topic = "tést" if str is not unicode: topic = topic.decode('utf8') p,s = self.create_bound_pair(zmq.PUB, zmq.SUB) self.assertEqual(s.send_unicode, s.send_unicode) self.assertEqual(p.recv_unicode, p.recv_unicode) self.assertRaises(TypeError, s.setsockopt, zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.setsockopt, zmq.IDENTITY, topic) s.setsockopt_unicode(zmq.IDENTITY, topic, 'utf16') self.assertRaises(TypeError, s.setsockopt, zmq.AFFINITY, topic) s.setsockopt_unicode(zmq.SUBSCRIBE, topic) self.assertRaises(TypeError, s.getsockopt_unicode, zmq.AFFINITY) self.assertRaisesErrno(zmq.EINVAL, s.getsockopt_unicode, zmq.SUBSCRIBE) identb = s.getsockopt(zmq.IDENTITY) identu = identb.decode('utf16') identu2 = s.getsockopt_unicode(zmq.IDENTITY, 'utf16') self.assertEqual(identu, identu2) time.sleep(0.1) # wait for connection/subscription p.send_unicode(topic,zmq.SNDMORE) p.send_unicode(topic*2, encoding='latin-1') self.assertEqual(topic, s.recv_unicode()) self.assertEqual(topic*2, s.recv_unicode(encoding='latin-1'))
Example #11
Source File: sockets.py From networkzero with MIT License | 6 votes |
def wait_for_news_from(self, address, topic, wait_for_s, is_raw=False): if isinstance(address, list): addresses = address else: addresses = [address] socket = self.get_socket(addresses, "subscriber") if isinstance(topic, str): topics = [topic] else: topics = topic for t in topics: socket.set(zmq.SUBSCRIBE, t.encode(config.ENCODING)) try: result = self._receive_with_timeout(socket, wait_for_s, use_multipart=True) unserialised_result = _unserialise_for_pubsub(result, is_raw) return unserialised_result except (core.SocketTimedOutError, core.SocketInterruptedError): return None, None
Example #12
Source File: socket.py From pySINDy with MIT License | 6 votes |
def set_string(self, option, optval, encoding='utf-8'): """Set socket options with a unicode object. This is simply a wrapper for setsockopt to protect from encoding ambiguity. See the 0MQ documentation for details on specific options. Parameters ---------- option : int The name of the option to set. Can be any of: SUBSCRIBE, UNSUBSCRIBE, IDENTITY optval : unicode string (unicode on py2, str on py3) The value of the option to set. encoding : str The encoding to be used, default is utf8 """ if not isinstance(optval, unicode): raise TypeError("unicode strings only") return self.set(option, optval.encode(encoding))
Example #13
Source File: heartmonitor.py From Computable with MIT License | 6 votes |
def __init__(self, in_addr, out_addr, mon_addr=None, in_type=zmq.SUB, out_type=zmq.DEALER, mon_type=zmq.PUB, heart_id=None): if mon_addr is None: self.device = ThreadDevice(zmq.FORWARDER, in_type, out_type) else: self.device = ThreadMonitoredQueue(in_type, out_type, mon_type, in_prefix=b"", out_prefix=b"") # do not allow the device to share global Context.instance, # which is the default behavior in pyzmq > 2.1.10 self.device.context_factory = zmq.Context self.device.daemon=True self.device.connect_in(in_addr) self.device.connect_out(out_addr) if mon_addr is not None: self.device.connect_mon(mon_addr) if in_type == zmq.SUB: self.device.setsockopt_in(zmq.SUBSCRIBE, b"") if heart_id is None: heart_id = uuid.uuid4().bytes self.device.setsockopt_out(zmq.IDENTITY, heart_id) self.id = heart_id
Example #14
Source File: test_monqueue.py From pySINDy with MIT License | 6 votes |
def build_device(self, mon_sub=b"", in_prefix=b'in', out_prefix=b'out'): self.device = devices.ThreadMonitoredQueue(zmq.PAIR, zmq.PAIR, zmq.PUB, in_prefix, out_prefix) alice = self.context.socket(zmq.PAIR) bob = self.context.socket(zmq.PAIR) mon = self.context.socket(zmq.SUB) aport = alice.bind_to_random_port('tcp://127.0.0.1') bport = bob.bind_to_random_port('tcp://127.0.0.1') mport = mon.bind_to_random_port('tcp://127.0.0.1') mon.setsockopt(zmq.SUBSCRIBE, mon_sub) self.device.connect_in("tcp://127.0.0.1:%i"%aport) self.device.connect_out("tcp://127.0.0.1:%i"%bport) self.device.connect_mon("tcp://127.0.0.1:%i"%mport) self.device.start() time.sleep(.2) try: # this is currenlty necessary to ensure no dropped monitor messages # see LIBZMQ-248 for more info mon.recv_multipart(zmq.NOBLOCK) except zmq.ZMQError: pass self.sockets.extend([alice, bob, mon]) return alice, bob, mon
Example #15
Source File: zeroless.py From python-zeroless with GNU Lesser General Public License v2.1 | 6 votes |
def sub(self, topics=(b'',)): """ Returns an iterable that can be used to iterate over incoming messages, that were published with one of the topics specified in ``topics``. Note that the iterable returns as many parts as sent by subscribed publishers. :param topics: a list of topics to subscribe to (default=b'') :type topics: list of bytes :rtype: generator """ sock = self.__sock(zmq.SUB) for topic in topics: if not isinstance(topic, bytes): error = 'Topics must be a list of bytes' log.error(error) raise TypeError(error) sock.setsockopt(zmq.SUBSCRIBE, topic) return self.__recv_generator(sock) # PushPull pattern
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: 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: TlmMQRecv.py From cFS-GroundSystem with Apache License 2.0 | 6 votes |
def main(): """ main method """ # Prepare our context and publisher context = zmq.Context() subscriber = context.socket(zmq.SUB) subscriber.connect("ipc:///tmp/GroundSystem") subscriber.setsockopt(zmq.SUBSCRIBE, b"GroundSystem") while True: try: # Read envelope with address address, contents = subscriber.recv_multipart() print(f"[{address}] {contents}") except KeyboardInterrupt: break # We never get here but clean up anyhow subscriber.close() context.term()
Example #19
Source File: supvisorszmq.py From supvisors with Apache License 2.0 | 5 votes |
def __init__(self, addresses, port): """ Initialization of the attributes. """ self.port = port self.socket = ZmqContext.socket(zmq.SUB) # connect all addresses for address in addresses: url = 'tcp://{}:{}'.format(address, self.port) self.socket.connect(url) self.socket.setsockopt(zmq.SUBSCRIBE, '')
Example #20
Source File: Client_Simple.py From EDDN with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(): context = zmq.Context() subscriber = context.socket(zmq.SUB) subscriber.setsockopt(zmq.SUBSCRIBE, "") while True: try: subscriber.connect(__relayEDDN) print 'Connect to EDDN' sys.stdout.flush() poller = zmq.Poller() poller.register(subscriber, zmq.POLLIN) while True: socks = dict(poller.poll(__timeoutEDDN)) if socks: if socks.get(subscriber) == zmq.POLLIN: __message = subscriber.recv(zmq.NOBLOCK) __message = zlib.decompress(__message) __json = simplejson.loads(__message) # call dumps() to ensure double quotes in output print simplejson.dumps(__json) sys.stdout.flush() else: print 'Disconnect from EDDN (After timeout)' sys.stdout.flush() subscriber.disconnect(__relayEDDN) break except zmq.ZMQError, e: print 'Disconnect from EDDN (After receiving ZMQError)' print 'ZMQSocketException: ' + str(e) sys.stdout.flush() subscriber.disconnect(__relayEDDN) time.sleep(10)
Example #21
Source File: vnrpc.py From InplusTrader_Linux with MIT License | 5 votes |
def subscribeTopic(self, topic): """ 订阅特定主题的广播数据 可以使用topic=''来订阅所有的主题 """ self.__socketSUB.setsockopt(zmq.SUBSCRIBE, topic) ########################################################################
Example #22
Source File: DWX_ZeroMQ_Connector_v2_0_1_RC8.py From DarwinexLabs with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _DWX_MTX_SUBSCRIBE_MARKETDATA_(self, _symbol, _string_delimiter=';'): # Subscribe to SYMBOL first. self._SUB_SOCKET.setsockopt_string(zmq.SUBSCRIBE, _symbol) if self._MarketData_Thread is None: self._MarketData_Thread = Thread(target=self._DWX_ZMQ_Poll_Data, args=(_string_delimiter)) self._MarketData_Thread.start() print("[KERNEL] Subscribed to {} BID/ASK updates. See self._Market_Data_DB.".format(_symbol))
Example #23
Source File: Client_Simple.py From EDDN with BSD 3-Clause "New" or "Revised" License | 5 votes |
def main(): context = zmq.Context() subscriber = context.socket(zmq.SUB) subscriber.setsockopt(zmq.SUBSCRIBE, b"") subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN) while True: try: subscriber.connect(__relayEDDN) while True: __message = subscriber.recv() if __message == False: subscriber.disconnect(__relayEDDN) break __message = zlib.decompress(__message) __json = simplejson.loads(__message) # call dumps() to ensure double quotes in output print(simplejson.dumps(__json)) sys.stdout.flush() except zmq.ZMQError as e: print ('ZMQSocketException: ' + str(e)) sys.stdout.flush() subscriber.disconnect(__relayEDDN) time.sleep(5)
Example #24
Source File: supvisorszmq.py From supvisors with Apache License 2.0 | 5 votes |
def subscribe_all(self): """ Subscription to all events. """ self.socket.setsockopt(zmq.SUBSCRIBE, '')
Example #25
Source File: supvisorszmq.py From supvisors with Apache License 2.0 | 5 votes |
def subscribe(self, code): """ Subscription to the event named code. """ self.socket.setsockopt(zmq.SUBSCRIBE, code.encode('utf-8')) # unsubscription part
Example #26
Source File: worker.py From rl_algorithms with MIT License | 5 votes |
def init_communication(self): """Initialize sockets connecting worker-learner, worker-buffer.""" # for receiving params from learner ctx = zmq.Context() self.sub_socket = ctx.socket(zmq.SUB) self.sub_socket.setsockopt_string(zmq.SUBSCRIBE, "") self.sub_socket.setsockopt(zmq.RCVHWM, 2) self.sub_socket.connect(f"tcp://127.0.0.1:{self.comm_cfg.learner_worker_port}") # for sending replay data to buffer self.push_socket = ctx.socket(zmq.PUSH) self.push_socket.connect(f"tcp://127.0.0.1:{self.comm_cfg.worker_buffer_port}")
Example #27
Source File: revocation_notifier.py From keylime with BSD 2-Clause "Simplified" License | 5 votes |
def start_broker(): def worker(): context = zmq.Context(1) frontend = context.socket(zmq.SUB) frontend.bind("ipc:///tmp/keylime.verifier.ipc") frontend.setsockopt(zmq.SUBSCRIBE, b'') # Socket facing services backend = context.socket(zmq.PUB) backend.bind("tcp://*:%s" % config.getint('cloud_verifier', 'revocation_notifier_port')) zmq.device(zmq.FORWARDER, frontend, backend) global broker_proc broker_proc = Process(target=worker) broker_proc.start()
Example #28
Source File: revocation_notifier.py From keylime with BSD 2-Clause "Simplified" License | 5 votes |
def await_notifications(callback, revocation_cert_path): global cert_key if revocation_cert_path is None: raise Exception("must specify revocation_cert_path") context = zmq.Context() mysock = context.socket(zmq.SUB) mysock.setsockopt(zmq.SUBSCRIBE, b'') mysock.connect("tcp://%s:%s" % (config.get('cloud_verifier', 'revocation_notifier_ip'), config.getint('cloud_verifier', 'revocation_notifier_port'))) logger.info('Waiting for revocation messages on 0mq %s:%s' % (config.get('cloud_verifier', 'revocation_notifier_ip'), config.getint('cloud_verifier', 'revocation_notifier_port'))) while True: rawbody = mysock.recv() body = json.loads(rawbody) if cert_key is None: # load up the CV signing public key if revocation_cert_path is not None and os.path.exists(revocation_cert_path): logger.info( "Lazy loading the revocation certificate from %s" % revocation_cert_path) with open(revocation_cert_path, 'r') as f: certpem = f.read() cert_key = crypto.x509_import_pubkey(certpem) if cert_key is None: logger.warning( "Unable to check signature of revocation message: %s not available" % revocation_cert_path) elif 'signature' not in body or body['signature'] == 'none': logger.warning("No signature on revocation message from server") elif not crypto.rsa_verify(cert_key, body['msg'].encode('utf-8'), body['signature'].encode('utf-8')): logger.error("Invalid revocation message siganture %s" % body) else: message = json.loads(body['msg']) logger.debug( "Revocation signature validated for revocation: %s" % message) callback(message)
Example #29
Source File: radar.py From gateway with GNU Affero General Public License v3.0 | 5 votes |
def feedback_loop(self, *args): # feedback socket ctx = zmq.Context() socket = ctx.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, "") socket.connect(config.get("radar-feedback-url", "tcp://localhost:7678")) print "radar feedback channel connected" while True: msg = [socket.recv()] while socket.getsockopt(zmq.RCVMORE): msg.append(socket.recv()) if len(msg) == 2: self.on_feedback_msg(*msg) else: print "bad feedback message", len(msg)
Example #30
Source File: broadcast.py From Jacinle with MIT License | 5 votes |
def initialize(self): self._context = zmq.Context() self._sock = self._context.socket(zmq.SUB) self._sock.set_hwm(BROADCAST_HWM) self._sock.connect(self._conn_info) self._sock.setsockopt(zmq.SUBSCRIBE, b'')