本文整理汇总了Python中zmq.SUBSCRIBE属性的典型用法代码示例。如果您正苦于以下问题:Python zmq.SUBSCRIBE属性的具体用法?Python zmq.SUBSCRIBE怎么用?Python zmq.SUBSCRIBE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类zmq
的用法示例。
在下文中一共展示了zmq.SUBSCRIBE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_string
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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))
示例2: test_unicode_sockopts
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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'))
示例3: build_device
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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
示例4: test_init_iface
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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)
示例5: test_init_socket
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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)
示例6: test_root_topic
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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)
示例7: main
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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()
示例8: wait_for_news_from
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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
示例9: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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
示例10: __init__
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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
示例11: _addBroker
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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)
示例12: sub
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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
示例13: start_broker
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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()
示例14: set_subscriber_topic
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
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')
示例15: subscriber
# 需要导入模块: import zmq [as 别名]
# 或者: from zmq import SUBSCRIBE [as 别名]
def subscriber(paths):
context = zmq.Context()
sock = context.socket(zmq.SUB)
# sock.connect("tcp://localhost:%s" % port)
for path in paths:
sock.connect("ipc://" + path)
sock.setsockopt(zmq.SUBSCRIBE, b"")
return context, sock