Python zmq.SUB Examples
The following are 30
code examples of zmq.SUB().
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 vnpy_crypto with MIT License | 7 votes |
def test_hwm(self): zmq3 = zmq.zmq_version_info()[0] >= 3 for stype in (zmq.PUB, zmq.ROUTER, zmq.SUB, zmq.REQ, zmq.DEALER): s = self.context.socket(stype) s.hwm = 100 self.assertEqual(s.hwm, 100) if zmq3: try: self.assertEqual(s.sndhwm, 100) except AttributeError: pass try: self.assertEqual(s.rcvhwm, 100) except AttributeError: pass s.close()
Example #2
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 #3
Source File: test_decorators.py From vnpy_crypto with MIT License | 6 votes |
def test_skt_multi_thread(): @socket(zmq.PUB) @socket(zmq.SUB) @socket(zmq.PUSH) def f(pub, sub, push): assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert isinstance(push, zmq.Socket), push assert pub.context is zmq.Context.instance() assert sub.context is zmq.Context.instance() assert push.context is zmq.Context.instance() assert pub.type == zmq.PUB assert sub.type == zmq.SUB assert push.type == zmq.PUSH assert len(set(map(id, [pub, sub, push]))) == 3 threads = [threading.Thread(target=f) for i in range(8)] [t.start() for t in threads] [t.join() for t in threads]
Example #4
Source File: test_decorators.py From vnpy_crypto with MIT License | 6 votes |
def test_multi_skts_single_ctx(): @context() @socket(zmq.PUB) @socket(zmq.SUB) @socket(zmq.PUSH) def test(ctx, pub, sub, push): assert isinstance(ctx, zmq.Context), ctx assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert isinstance(push, zmq.Socket), push assert pub.context is ctx assert sub.context is ctx assert push.context is ctx assert pub.type == zmq.PUB assert sub.type == zmq.SUB assert push.type == zmq.PUSH test()
Example #5
Source File: test_decorators.py From pySINDy with MIT License | 6 votes |
def test_skt_multi_thread(): @socket(zmq.PUB) @socket(zmq.SUB) @socket(zmq.PUSH) def f(pub, sub, push): assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert isinstance(push, zmq.Socket), push assert pub.context is zmq.Context.instance() assert sub.context is zmq.Context.instance() assert push.context is zmq.Context.instance() assert pub.type == zmq.PUB assert sub.type == zmq.SUB assert push.type == zmq.PUSH assert len(set(map(id, [pub, sub, push]))) == 3 threads = [threading.Thread(target=f) for i in range(8)] [t.start() for t in threads] [t.join() for t in threads]
Example #6
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 #7
Source File: test_decorators.py From vnpy_crypto with MIT License | 6 votes |
def multi_skts_method_other_args(self): @socket(zmq.PUB) @socket(zmq.SUB) def f(foo, pub, sub, bar=None): assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert foo == 'mock' assert bar == 'fake' assert pub.context is zmq.Context.instance() assert sub.context is zmq.Context.instance() assert pub.type is zmq.PUB assert sub.type is zmq.SUB f('mock', bar='fake')
Example #8
Source File: test_log.py From pySINDy 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 #9
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 #10
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 #11
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 #12
Source File: test_decorators.py From pySINDy with MIT License | 6 votes |
def test_multi_skts_with_name(): @socket('foo', zmq.PUSH) @socket('bar', zmq.SUB) @socket('baz', zmq.PUB) def test(foo, bar, baz): assert isinstance(foo, zmq.Socket), foo assert isinstance(bar, zmq.Socket), bar assert isinstance(baz, zmq.Socket), baz assert foo.context is zmq.Context.instance() assert bar.context is zmq.Context.instance() assert baz.context is zmq.Context.instance() assert foo.type == zmq.PUSH assert bar.type == zmq.SUB assert baz.type == zmq.PUB test()
Example #13
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 #14
Source File: test_decorators.py From pySINDy with MIT License | 6 votes |
def multi_skts_method_other_args(self): @socket(zmq.PUB) @socket(zmq.SUB) def f(foo, pub, sub, bar=None): assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert foo == 'mock' assert bar == 'fake' assert pub.context is zmq.Context.instance() assert sub.context is zmq.Context.instance() assert pub.type is zmq.PUB assert sub.type is zmq.SUB f('mock', bar='fake')
Example #15
Source File: test_decorators.py From pySINDy with MIT License | 6 votes |
def test_multi_skts_single_ctx(): @context() @socket(zmq.PUB) @socket(zmq.SUB) @socket(zmq.PUSH) def test(ctx, pub, sub, push): assert isinstance(ctx, zmq.Context), ctx assert isinstance(pub, zmq.Socket), pub assert isinstance(sub, zmq.Socket), sub assert isinstance(push, zmq.Socket), push assert pub.context is ctx assert sub.context is ctx assert push.context is ctx assert pub.type == zmq.PUB assert sub.type == zmq.SUB assert push.type == zmq.PUSH test()
Example #16
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 #17
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 #18
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 #19
Source File: vnrpc.py From vnpy_crypto with MIT License | 6 votes |
def __init__(self, reqAddress, subAddress): """Constructor""" super(RpcClient, self).__init__() # zmq端口相关 self.__reqAddress = reqAddress self.__subAddress = subAddress self.__context = zmq.Context() self.__socketREQ = self.__context.socket(zmq.REQ) # 请求发出socket self.__socketSUB = self.__context.socket(zmq.SUB) # 广播订阅socket # 工作线程相关,用于处理服务器推送的数据 self.__active = False # 客户端的工作状态 self.__thread = threading.Thread(target=self.run) # 客户端的工作线程 #----------------------------------------------------------------------
Example #20
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 #21
Source File: test_socket.py From pySINDy with MIT License | 6 votes |
def test_hwm(self): zmq3 = zmq.zmq_version_info()[0] >= 3 for stype in (zmq.PUB, zmq.ROUTER, zmq.SUB, zmq.REQ, zmq.DEALER): s = self.context.socket(stype) s.hwm = 100 self.assertEqual(s.hwm, 100) if zmq3: try: self.assertEqual(s.sndhwm, 100) except AttributeError: pass try: self.assertEqual(s.rcvhwm, 100) except AttributeError: pass s.close()
Example #22
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 #23
Source File: zmq_client.py From backend with GNU General Public License v2.0 | 6 votes |
def __init__(self, zmq_context, trade_in_socket, trade_pub = None, reopen=True): self.zmq_context = zmq_context self.connection_id = None self.trade_in_socket = trade_in_socket self.is_logged = False self.user_id = None self.reopen = reopen self.trade_pub = trade_pub self.trade_pub_socket = None self.trade_pub_socket_stream = None if self.trade_pub: self.trade_pub_socket = self.zmq_context.socket(zmq.SUB) self.trade_pub_socket.connect(self.trade_pub) self.trade_pub_socket_stream = ZMQStream(self.trade_pub_socket) self.trade_pub_socket_stream.on_recv(self._on_trade_publish)
Example #24
Source File: zmq_client.py From backend with GNU General Public License v2.0 | 6 votes |
def connect(self): if not self.trade_pub_socket and self.trade_pub: self.trade_pub_socket = self.zmq_context.socket(zmq.SUB) self.trade_pub_socket.connect(self.trade_pub) self.trade_pub_socket_stream = ZMQStream(self.trade_pub_socket) self.trade_pub_socket_stream.on_recv(self._on_trade_publish) self.trade_in_socket.send( "OPN," + base64.b32encode(os.urandom(10))) response_message = self.trade_in_socket.recv() opt_code = response_message[:3] raw_message = response_message[4:] if opt_code != 'OPN': if opt_code == 'ERR': raise TradeClientException( error_message = raw_message ) raise TradeClientException( error_message = 'Protocol Error: Unknown message opt_code received' ) self.connection_id = raw_message
Example #25
Source File: test_zeromq.py From spruned with MIT License | 6 votes |
def _subscribe_topic(self, topic, url, max_msgs): with Context() as ctx: socket = ctx.socket(zmq.SUB) socket.connect(url) socket.subscribe(topic) if not self._data_from_topics.get(topic): self._data_from_topics[topic] = [] x = 0 while x < max_msgs: x += 1 print('waiting') msg = await socket.recv_multipart() print('done') self._data_from_topics[topic].append(msg) socket.close() print('socket closed')
Example #26
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 #27
Source File: channels.py From Computable with MIT License | 6 votes |
def flush(self, timeout=1.0): """Immediately processes all pending messages on the iopub channel. Callers should use this method to ensure that :method:`call_handlers` has been called for all messages that have been received on the 0MQ SUB socket of this channel. This method is thread safe. Parameters ---------- timeout : float, optional The maximum amount of time to spend flushing, in seconds. The default is one second. """ # We do the IOLoop callback process twice to ensure that the IOLoop # gets to perform at least one full poll. stop_time = time.time() + timeout for i in xrange(2): self._flushed = False self.ioloop.add_callback(self._flush) while not self._flushed and time.time() < stop_time: time.sleep(0.01)
Example #28
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 #29
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 #30
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