Python zmq.PUB Examples
The following are 30
code examples of zmq.PUB().
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: vnrpc.py From vnpy_crypto with MIT License | 6 votes |
def __init__(self, repAddress, pubAddress): """Constructor""" # 调用RpcObject的构造器:self.usePickle() super(RpcServer, self).__init__() # 保存功能函数的字典,key是函数名,value是函数对象 self.__functions = {} # zmq端口相关 self.__context = zmq.Context() self.__socketREP = self.__context.socket(zmq.REP) # 请求回应socket self.__socketREP.bind(repAddress) self.__socketPUB = self.__context.socket(zmq.PUB) # 数据广播socket self.__socketPUB.bind(pubAddress) # 工作线程相关 self.__active = False # 服务器的工作状态 self.__thread = threading.Thread(target=self.run) # 服务器的工作线程 #----------------------------------------------------------------------
Example #3
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 #4
Source File: revocation_notifier.py From keylime with BSD 2-Clause "Simplified" License | 6 votes |
def notify(tosend): def worker(tosend): context = zmq.Context() mysock = context.socket(zmq.PUB) mysock.connect("ipc:///tmp/keylime.verifier.ipc") # wait 100ms for connect to happen time.sleep(0.2) # now send it out vi 0mq logger.info("Sending revocation event to listening nodes..") for i in range(config.getint('cloud_verifier', 'max_retries')): try: mysock.send_string(json.dumps(tosend)) break except Exception as e: logger.debug("Unable to publish revocation message %d times, trying again in %f seconds: %s" % ( i, config.getfloat('cloud_verifier', 'retry_interval'), e)) time.sleep(config.getfloat('cloud_verifier', 'retry_interval')) mysock.close() cb = functools.partial(worker, tosend) t = threading.Thread(target=cb) t.start()
Example #5
Source File: test_worker.py From westpa with MIT License | 6 votes |
def setUp(self): super(TestZMQWorkerBasic,self).setUp() self.rr_endpoint = self.make_endpoint() self.ann_endpoint = self.make_endpoint() # Need to bind ann_socket here in setup, because if we bind it during # tests, messages get lost. self.ann_socket = self.test_context.socket(zmq.PUB) self.ann_socket.bind(self.ann_endpoint) # If we're binding ann_socket, we might as well bind rr_socket self.rr_socket = self.test_context.socket(zmq.REP) self.rr_socket.bind(self.rr_endpoint) self.test_worker = ZMQWorker(self.rr_endpoint, self.ann_endpoint) self.test_worker.validation_fail_action = 'raise' self.test_worker.shutdown_timeout = 0.5 self.test_worker.master_beacon_period = BEACON_PERIOD self.test_worker.startup() self.test_core.master_id = self.test_core.node_id time.sleep(SETUP_WAIT)
Example #6
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 #7
Source File: test_context.py From vnpy_crypto with MIT License | 6 votes |
def test_shadow(self): ctx = self.Context() ctx2 = self.Context.shadow(ctx.underlying) self.assertEqual(ctx.underlying, ctx2.underlying) s = ctx.socket(zmq.PUB) s.close() del ctx2 self.assertFalse(ctx.closed) s = ctx.socket(zmq.PUB) ctx2 = self.Context.shadow(ctx.underlying) s2 = ctx2.socket(zmq.PUB) s.close() s2.close() ctx.term() self.assertRaisesErrno(zmq.EFAULT, ctx2.socket, zmq.PUB) del ctx2
Example #8
Source File: test_decorators.py From vnpy_crypto with MIT License | 6 votes |
def test_skt_reinit(): result = {'foo': None, 'bar': None} @socket(zmq.PUB) def f(key, skt): assert isinstance(skt, zmq.Socket), skt result[key] = skt foo_t = threading.Thread(target=f, args=('foo',)) bar_t = threading.Thread(target=f, args=('bar',)) foo_t.start() bar_t.start() foo_t.join() bar_t.join() assert result['foo'] is not None, result assert result['bar'] is not None, result assert result['foo'] is not result['bar'], result
Example #9
Source File: kernelapp.py From Computable with MIT License | 6 votes |
def init_sockets(self): # Create a context, a session, and the kernel sockets. self.log.info("Starting the kernel at pid: %i", os.getpid()) context = zmq.Context.instance() # Uncomment this to try closing the context. # atexit.register(context.term) self.shell_socket = context.socket(zmq.ROUTER) self.shell_port = self._bind_socket(self.shell_socket, self.shell_port) self.log.debug("shell ROUTER Channel on port: %i" % self.shell_port) self.iopub_socket = context.socket(zmq.PUB) self.iopub_port = self._bind_socket(self.iopub_socket, self.iopub_port) self.log.debug("iopub PUB Channel on port: %i" % self.iopub_port) self.stdin_socket = context.socket(zmq.ROUTER) self.stdin_port = self._bind_socket(self.stdin_socket, self.stdin_port) self.log.debug("stdin ROUTER Channel on port: %i" % self.stdin_port) self.control_socket = context.socket(zmq.ROUTER) self.control_port = self._bind_socket(self.control_socket, self.control_port) self.log.debug("control ROUTER Channel on port: %i" % self.control_port)
Example #10
Source File: test_decorators.py From vnpy_crypto with MIT License | 6 votes |
def test_multi_skts(): @socket(zmq.PUB) @socket(zmq.SUB) @socket(zmq.PUSH) def test(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 test()
Example #11
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 #12
Source File: test_decorators.py From vnpy_crypto 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_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 #14
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 #15
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 #16
Source File: test_utils.py From spyder-kernels with MIT License | 6 votes |
def get_kernel(kernel_class=SpyderKernel): """Get an instance of a kernel with the kernel class given.""" log = logging.getLogger('test') log.setLevel(logging.DEBUG) for hdlr in log.handlers: log.removeHandler(hdlr) hdlr = logging.StreamHandler(StringIO()) hdlr.setLevel(logging.DEBUG) log.addHandler(hdlr) context = zmq.Context.instance() iopub_socket = context.socket(zmq.PUB) kernel = kernel_class(session=ss.Session(), iopub_socket=iopub_socket, log=log) return kernel
Example #17
Source File: RoutingService.py From cFS-GroundSystem with Apache License 2.0 | 6 votes |
def __init__(self): super().__init__() # Init lists self.ipAddressesList = ["All"] self.spacecraftNames = ["All"] self.specialPktId = [] self.specialPktName = [] self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Init zeroMQ self.context = zmq.Context() self.publisher = self.context.socket(zmq.PUB) self.publisher.bind("ipc:///tmp/GroundSystem") # Run thread
Example #18
Source File: util.py From Computable with MIT License | 5 votes |
def connect_logger(logname, context, iface, root="ip", loglevel=logging.DEBUG): logger = logging.getLogger(logname) if any([isinstance(h, handlers.PUBHandler) for h in logger.handlers]): # don't add a second PUBHandler return loglevel = integer_loglevel(loglevel) lsock = context.socket(zmq.PUB) lsock.connect(iface) handler = handlers.PUBHandler(lsock) handler.setLevel(loglevel) handler.root_topic = root logger.addHandler(handler) logger.setLevel(loglevel)
Example #19
Source File: proxydevice.py From Computable with MIT License | 5 votes |
def __init__(self, in_type, out_type, mon_type=zmq.PUB): Device.__init__(self, in_type=in_type, out_type=out_type) self.mon_type = mon_type self._mon_binds = [] self._mon_connects = [] self._mon_sockopts = []
Example #20
Source File: test_socket.py From pySINDy with MIT License | 5 votes |
def test_bind_unicode(self): s = self.socket(zmq.PUB) p = s.bind_to_random_port(unicode("tcp://*"))
Example #21
Source File: test_socket.py From pySINDy with MIT License | 5 votes |
def test_connect_unicode(self): s = self.socket(zmq.PUB) s.connect(unicode("tcp://127.0.0.1:5555"))
Example #22
Source File: util.py From derplearning with MIT License | 5 votes |
def publisher(path): context = zmq.Context() sock = context.socket(zmq.PUB) sock.bind("ipc://" + path) # sock.bind("tcp://*:%s" % port) return context, sock
Example #23
Source File: zeroless.py From python-zeroless with GNU Lesser General Public License v2.1 | 5 votes |
def _setup(self, sock): if sock.socket_type == zmq.SUB: warning = 'SUB sockets that bind will not get any message before ' warning += 'they first ask for via the provided generator, so ' warning += 'prefer to bind PUB sockets if missing some messages ' warning += 'is not an option' warn(warning) self._port = _bind_zmq_sock(sock, self._port)
Example #24
Source File: handlers.py From Computable with MIT License | 5 votes |
def __init__(self, interface_or_socket, context=None): logging.Handler.__init__(self) if isinstance(interface_or_socket, zmq.Socket): self.socket = interface_or_socket self.ctx = self.socket.context else: self.ctx = context or zmq.Context() self.socket = self.ctx.socket(zmq.PUB) self.socket.bind(interface_or_socket)
Example #25
Source File: test_socket.py From pySINDy with MIT License | 5 votes |
def test_dir(self): ctx = self.Context() s = ctx.socket(zmq.PUB) self.assertTrue('send' in dir(s)) self.assertTrue('IDENTITY' in dir(s)) self.assertTrue('AFFINITY' in dir(s)) self.assertTrue('FD' in dir(s)) s.close() ctx.term()
Example #26
Source File: ipcontrollerapp.py From Computable with MIT License | 5 votes |
def forward_logging(self): if self.log_url: self.log.info("Forwarding logging to %s"%self.log_url) context = zmq.Context.instance() lsock = context.socket(zmq.PUB) lsock.connect(self.log_url) handler = PUBHandler(lsock) handler.root_topic = 'controller' handler.setLevel(self.log_level) self.log.addHandler(handler)
Example #27
Source File: ipengineapp.py From Computable with MIT License | 5 votes |
def bind_kernel(self, **kwargs): """Promote engine to listening kernel, accessible to frontends.""" if self.kernel_app is not None: return self.log.info("Opening ports for direct connections as an IPython kernel") kernel = self.kernel kwargs.setdefault('config', self.config) kwargs.setdefault('log', self.log) kwargs.setdefault('profile_dir', self.profile_dir) kwargs.setdefault('session', self.engine.session) app = self.kernel_app = IPKernelApp(**kwargs) # allow IPKernelApp.instance(): IPKernelApp._instance = app app.init_connection_file() # relevant contents of init_sockets: app.shell_port = app._bind_socket(kernel.shell_streams[0], app.shell_port) app.log.debug("shell ROUTER Channel on port: %i", app.shell_port) app.iopub_port = app._bind_socket(kernel.iopub_socket, app.iopub_port) app.log.debug("iopub PUB Channel on port: %i", app.iopub_port) kernel.stdin_socket = self.engine.context.socket(zmq.ROUTER) app.stdin_port = app._bind_socket(kernel.stdin_socket, app.stdin_port) app.log.debug("stdin ROUTER Channel on port: %i", app.stdin_port) # start the heartbeat, and log connection info: app.init_heartbeat() app.log_connection_info() app.write_connection_file()
Example #28
Source File: test_socket.py From pySINDy with MIT License | 5 votes |
def test_create(self): ctx = self.Context() s = ctx.socket(zmq.PUB) # Superluminal protocol not yet implemented self.assertRaisesErrno(zmq.EPROTONOSUPPORT, s.bind, 'ftl://a') self.assertRaisesErrno(zmq.EPROTONOSUPPORT, s.connect, 'ftl://a') self.assertRaisesErrno(zmq.EINVAL, s.bind, 'tcp://') s.close() del ctx
Example #29
Source File: handlers.py From pySINDy with MIT License | 5 votes |
def __init__(self, interface_or_socket, context=None): logging.Handler.__init__(self) if isinstance(interface_or_socket, zmq.Socket): self.socket = interface_or_socket self.ctx = self.socket.context else: self.ctx = context or zmq.Context() self.socket = self.ctx.socket(zmq.PUB) self.socket.bind(interface_or_socket)
Example #30
Source File: util.py From Computable with MIT License | 5 votes |
def connect_engine_logger(context, iface, engine, loglevel=logging.DEBUG): logger = logging.getLogger() if any([isinstance(h, handlers.PUBHandler) for h in logger.handlers]): # don't add a second PUBHandler return loglevel = integer_loglevel(loglevel) lsock = context.socket(zmq.PUB) lsock.connect(iface) handler = EnginePUBHandler(engine, lsock) handler.setLevel(loglevel) logger.addHandler(handler) logger.setLevel(loglevel) return logger