我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用zmq.PUSH。
def notify_msg(self, type, price): import zmq try: context = zmq.Context() socket = context.socket(zmq.PUSH) socket.connect ("tcp://%s:%s" % (config.ZMQ_HOST, config.ZMQ_PORT)) time.sleep(1) message = {'type':type, 'price':price} logging.info( "notify message %s", json.dumps(message)) socket.send_string(json.dumps(message)) except Exception as e: logging.warn("notify_msg Exception") pass
def __register_sockets(self): """ Registers ZMQ sockets for communication with other processes in Enarksh. """ config = Config.get() # Register socket for receiving asynchronous incoming messages. self.__message_controller.register_end_point('pull', zmq.PULL, config.get_spawner_pull_end_point()) # Register socket for sending asynchronous messages to the controller. self.__message_controller.register_end_point('controller', zmq.PUSH, config.get_controller_pull_end_point()) # Register socket for sending asynchronous messages to the logger. self.__message_controller.register_end_point('logger', zmq.PUSH, config.get_logger_pull_end_point()) # ------------------------------------------------------------------------------------------------------------------
def __register_sockets(self): """ Registers ZMQ sockets for communication with other processes in Enarksh. """ config = Config.get() # Register socket for receiving asynchronous incoming messages. self.message_controller.register_end_point('pull', zmq.PULL, config.get_controller_pull_end_point()) # Create socket for lockstep incoming messages. self.message_controller.register_end_point('lockstep', zmq.REP, config.get_controller_lockstep_end_point()) # Create socket for sending asynchronous messages to the spanner. self.message_controller.register_end_point('spawner', zmq.PUSH, config.get_spawner_pull_end_point()) # Create socket for sending asynchronous messages to the logger. self.message_controller.register_end_point('logger', zmq.PUSH, config.get_logger_pull_end_point()) # ------------------------------------------------------------------------------------------------------------------
def register_end_point(self, name, socket_type, end_point): """ Registers an end point. :param str name: The name of the end point. :param int socket_type: The socket type, one of - zmq.PULL for asynchronous incoming messages - zmq.REP for lockstep incoming messages - zmq.PUSH for asynchronous outgoing messages :param str end_point: The end point. """ socket = self.__zmq_context.socket(socket_type) self.__end_points[name] = socket if socket_type in [zmq.PULL, zmq.REP]: socket.bind(end_point) elif socket_type == zmq.PUSH: socket.connect(end_point) else: raise ValueError("Unknown socket type {0}".format(socket_type)) # ------------------------------------------------------------------------------------------------------------------
def test_shadow_pyczmq(self): try: from pyczmq import zctx, zsocket except Exception: raise SkipTest("Requires pyczmq") ctx = zctx.new() ca = zsocket.new(ctx, zmq.PUSH) cb = zsocket.new(ctx, zmq.PULL) a = zmq.Socket.shadow(ca) b = zmq.Socket.shadow(cb) a.bind("inproc://a") b.connect("inproc://a") a.send(b'hi') rcvd = self.recv(b) self.assertEqual(rcvd, b'hi') # Travis can't handle how much memory PyPy uses on this test
def test_gc(self): """test close&term by garbage collection alone""" if PYPY: raise SkipTest("GC doesn't work ") # test credit @dln (GH #137): def gcf(): def inner(): ctx = self.Context() s = ctx.socket(zmq.PUSH) inner() gc.collect() t = Thread(target=gcf) t.start() t.join(timeout=1) self.assertFalse(t.is_alive(), "Garbage collection should have cleaned up context")
def test_shadow_pyczmq(self): try: from pyczmq import zctx, zsocket, zstr except Exception: raise SkipTest("Requires pyczmq") ctx = zctx.new() a = zsocket.new(ctx, zmq.PUSH) zsocket.bind(a, "inproc://a") ctx2 = self.Context.shadow_pyczmq(ctx) b = ctx2.socket(zmq.PULL) b.connect("inproc://a") zstr.send(a, b'hi') rcvd = self.recv(b) self.assertEqual(rcvd, b'hi') b.close()
def test_poll(self): @gen.coroutine def test(): a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL) f = b.poll(timeout=0) self.assertEqual(f.result(), 0) f = b.poll(timeout=1) assert not f.done() evt = yield f self.assertEqual(evt, 0) f = b.poll(timeout=1000) assert not f.done() yield a.send_multipart([b'hi', b'there']) evt = yield f self.assertEqual(evt, zmq.POLLIN) recvd = yield b.recv_multipart() self.assertEqual(recvd, [b'hi', b'there']) self.loop.run_sync(test)
def test_pushpull_raw_zmq_outside(nsproxy): """ Simple push-pull pattern test. Channel without serialization. The message is sent from outside osBrain, through a ZMQ PUSH socket. """ # Create an osBrain agent that will receive the message a1 = run_agent('a1') a1.set_attr(received=None) addr = a1.bind('PULL', transport='tcp', handler=set_received, serializer='raw') # Create a raw ZeroMQ PUSH socket context = zmq.Context() socket = context.socket(zmq.PUSH) socket.connect('tcp://%s:%s' % (addr.address.host, addr.address.port)) # Send the message message = b'Hello world' socket.send(message) assert wait_agent_attr(a1, name='received', value=message) socket.close() context.destroy()
def router_main(_, pidx, args): log = get_logger('examples.zmqserver.extra', pidx) ctx = zmq.Context() ctx.linger = 0 in_sock = ctx.socket(zmq.PULL) in_sock.bind('tcp://*:5000') out_sock = ctx.socket(zmq.PUSH) out_sock.bind('ipc://example-events') try: log.info('router proxy started') zmq.proxy(in_sock, out_sock) except KeyboardInterrupt: pass except: log.exception('unexpected error') finally: log.info('router proxy terminated') in_sock.close() out_sock.close() ctx.term()
def reset(self): self.status = READY context = zmq.Context() self._socket1 = context.socket(zmq.PUSH) self._socket1.bind(self._address1) self._socket1.set_hwm(32) self._socket2 = context.socket(zmq.PULL) self._socket2.set_hwm(32) self._socket2.RCVTIMEO = 1 self._socket2.bind(self._address2) self._prev_drained = False self._sub_drained = False self._conn1_send_count = 0 self._conn1_recv_count = {} self._conn2_send_count = {} self._conn2_recv_count = 0 self._retry_count = 0
def reset(self): self.status = READY context = zmq.Context() self._socket = context.socket(zmq.PULL) self._socket.RCVTIMEO = 1 sync_socket = context.socket(zmq.PUSH) while self._ports['conn1'] is None or self._ports['sync_conn1'] is None: sleep(0.01) # Handshake with main process self._socket.connect(self._address + ':' + str(self._ports['conn1'])) sync_socket.connect(self._address + ':' + str(self._ports['sync_conn1'])) packet = msgpack.dumps(b'SYNC') sync_socket.send(packet) sync_socket.close() self._num_recv = 0 self._drained = False
def prepare(): config = Config() global tee global input_files_dir global result_files_dir context = zmq.Context() logger_socket = context.socket(zmq.PUSH) logger_socket.connect(config.server_log['external_url']) tee = logger_socket.send_string atexit.register(close_sockets, [logger_socket]) input_files_dir = os.path.expanduser(config.server_files['input_files_dir']) result_files_dir = os.path.expanduser(config.server_files['result_files_dir']) tee('Started service files with pid {}'.format(os.getpid())) return config
def zmq_streamer(): try: context = zmq.Context() # Socket facing clients frontend = context.socket(zmq.PUSH) frontend.bind("tcp://*:%s" % (zmq_queue_port_push)) # Socket facing services backend = context.socket(zmq.PULL) backend.bind("tcp://*:%s" % (zmq_queue_port_pull)) zmq.device(zmq.STREAMER, frontend, backend) except Exception as e: print(e) print("bringing down zmq device") finally: frontend.close() backend.close() context.term()
def __init__(self, push, pull, redis_conf): super(MinerClient, self).__init__() print("Connecting to Redis cache {} ...".format(redis_conf)) redis_host, redis_port, redis_db = redis_conf.split(":") self.redis = redis.StrictRedis(host=redis_host, port=int(redis_port), db=int(redis_db)) self.redis.setnx('transaction', 0) # NOTE: Expiration times for pending/processed tasks in seconds. self.transaction_expiration = 60 * 60 self.result_expiration = 60 * 10 context = zmq.Context() print("Connecting to push socket '{}' ...".format(push)) self.push = context.socket(zmq.PUSH) self.push.connect(push) print("Binding to pull socket '{}' ...".format(pull)) self.pull = context.socket(zmq.PULL) self.pull.bind(pull)
def test_tcp_push_socket(event_loop, socket_factory, connect_or_bind): pull_socket = socket_factory.create(zmq.PULL) connect_or_bind(pull_socket, 'tcp://127.0.0.1:3333', reverse=True) def run(): assert pull_socket.poll(1000) == zmq.POLLIN message = pull_socket.recv_multipart() assert message == [b'hello', b'world'] with run_in_background(run) as event: async with azmq.Context(loop=event_loop) as context: socket = context.socket(azmq.PUSH) connect_or_bind(socket, 'tcp://127.0.0.1:3333') await socket.send_multipart([b'hello', b'world']) while not event.is_set(): await asyncio.sleep(0.1)
def test_shadow_pyczmq(self): try: from pyczmq import zctx, zsocket except Exception: raise SkipTest("Requires pyczmq") ctx = zctx.new() ca = zsocket.new(ctx, zmq.PUSH) cb = zsocket.new(ctx, zmq.PULL) a = zmq.Socket.shadow(ca) b = zmq.Socket.shadow(cb) a.bind("inproc://a") b.connect("inproc://a") a.send(b'hi') rcvd = self.recv(b) self.assertEqual(rcvd, b'hi')
def execute_command_streamer(): from oldspeak.console.parsers.streamer import parser args = parser.parse_args(get_sub_parser_argv()) bootstrap_conf_with_gevent(args) device = Device(zmq.STREAMER, zmq.PULL, zmq.PUSH) device.bind_in(args.pull) device.bind_out(args.push) if args.pull_hwm: device.setsockopt_in(zmq.RCVHWM, args.pull_hwm) if args.push_hwm: device.setsockopt_out(zmq.SNDHWM, args.push_hwm) print "oldspeak streamer started" print "date", datetime.utcnow().isoformat() print "pull", (getattr(args, 'pull')) print "push", (getattr(args, 'push')) device.start()
def __init__(self, url, pattern=ZmqfPattern.MPUP): ''' ''' protocol, host, port, uri = zmqf_utils.parse_url(url) self.context = zmq.Context() self.pattern = pattern if self.pattern == ZmqfPattern.MPBS: self._socket = self.context.socket(zmq.PUB) # @UndefinedVariable self._socket.connect('%s://%s:%s'% (protocol, host, port)) time.sleep(0.25) elif self.pattern == ZmqfPattern.MPUP: self._socket = self.context.socket(zmq.PUSH) # @UndefinedVariable self._socket.connect('%s://%s:%s'% (protocol, host, port)) elif self.pattern == ZmqfPattern.MRER: self._socket = self.context.socket(zmq.REQ) # @UndefinedVariable self._socket.connect('%s://%s:%s'% (protocol, host, port))
def consumer(): consumer_id = random.randrange(1,10005) print "I am consumer #%s" % (consumer_id) context = zmq.Context() # recieve work consumer_receiver = context.socket(zmq.PULL) consumer_receiver.connect("tcp://127.0.0.1:5557") # send work consumer_sender = context.socket(zmq.PUSH) consumer_sender.connect("tcp://127.0.0.1:5558") while True: work = consumer_receiver.recv_json() data = work['num'] result = { 'consumer' : consumer_id, 'num' : data} if data%2 == 0: consumer_sender.send_json(result)
def main(): try: context = zmq.Context(1) # Socket facing clients frontend = context.socket(zmq.PULL) frontend.bind("tcp://*:5559") # Socket facing services backend = context.socket(zmq.PUSH) backend.bind("tcp://*:5560") zmq.device(zmq.STREAMER, frontend, backend) except Exception, e: print e print "bringing down zmq device" finally: pass frontend.close() backend.close() context.term()
def __init__(self, config): super().__init__(config) self.paused = False slave_queue = self.ctx.socket(zmq.ROUTER) slave_queue.ipv6 = True slave_queue.bind(config.slave_queue) self.register(slave_queue, self.handle_slave) self.status_queue = self.ctx.socket(zmq.PUSH) self.status_queue.hwm = 10 self.status_queue.connect(const.INT_STATUS_QUEUE) SlaveState.status_queue = self.status_queue self.builds_queue = self.ctx.socket(zmq.REQ) self.builds_queue.hwm = 1 self.builds_queue.connect(config.builds_queue) self.index_queue = self.ctx.socket(zmq.PUSH) self.index_queue.hwm = 10 self.index_queue.connect(config.index_queue) self.db = DbClient(config) self.fs = FsClient(config) self.slaves = {} self.pypi_simple = config.pypi_simple
def test_send_1k_push_pull(self): down, up, port = self.create_bound_pair(zmq.PUSH, zmq.PULL) eventlet.sleep() done = eventlet.Event() def tx(): tx_i = 0 while tx_i <= 1000: tx_i += 1 down.send(str(tx_i).encode()) def rx(): while True: rx_i = up.recv() if rx_i == b"1000": done.send(0) break eventlet.spawn(tx) eventlet.spawn(rx) final_i = done.wait() self.assertEqual(final_i, 0)
def serve_data(ds, addr): ctx = zmq.Context() socket = ctx.socket(zmq.PUSH) socket.set_hwm(10) socket.bind(addr) ds = RepeatedData(ds, -1) try: ds.reset_state() logger.info("Serving data at {}".format(addr)) while True: for dp in ds.get_data(): socket.send(dumps(dp), copy=False) finally: socket.setsockopt(zmq.LINGER, 0) socket.close() if not ctx.closed: ctx.destroy(0)
def run(self): player = self._build_player() context = zmq.Context() c2s_socket = context.socket(zmq.PUSH) c2s_socket.setsockopt(zmq.IDENTITY, self.identity) c2s_socket.set_hwm(2) c2s_socket.connect(self.c2s) s2c_socket = context.socket(zmq.DEALER) s2c_socket.setsockopt(zmq.IDENTITY, self.identity) #s2c_socket.set_hwm(5) s2c_socket.connect(self.s2c) state = player.current_state() reward, isOver = 0, False while True: c2s_socket.send(dumps( (self.identity, state, reward, isOver)), copy=False) action = loads(s2c_socket.recv(copy=False).bytes) reward, isOver = player.action(action) state = player.current_state() # compatibility
def __init__(self, opts=None): if opts is None: self.opts = self.process_config(CONFIG_LOCATION) else: self.opts = opts return # General setup of ZeroMQ self.ctx = zmq.Context() self.loop = zmq.eventloop.IOLoop.instance() # Begin setup of PULL socket self.pull_socket = self.ctx.socket(zmq.PULL) self.pull_socket.bind('tcp://127.0.0.1:2001') self.pull_stream = zmq.eventloop.zmqstream.ZMQStream(self.pull_socket, self.loop) self.pull_stream.on_recv(self.stream_decode) # Begin setup of PUSH socket for IPC to publisher self.push_socket = self.ctx.socket(zmq.PUSH) self.push_socket.connect('ipc:///tmp/reactor.ipc') self.push_stream = zmq.eventloop.zmqstream.ZMQStream(self.push_socket, self.loop) self.actions = loader.load_actions(self.opts, '/home/mp/devel/eventdriventalk/actions') self.registers = loader.load_registers(self.opts, '/home/mp/devel/eventdriventalk/registers') self.rules = loader.load_registers(self.opts, '/home/mp/devel/eventdriventalk/rules')
def _make_eventer(): ctx = zmq.Context() push_socket = self.ctx.socket(zmq.PUSH) push_socket.connect('ipc:///tmp/reactor.ipc') EVENT_SOCKET = push_socket
def handle(self): """ Executes the halt command. """ self.output = EnarkshStyle(self.input, self.output) os.chdir(C.HOME) self.__read_config() self.__zmq_init() daemon = self.input.get_argument('daemon') if daemon not in self.__end_points: self.output.error("Unknown daemon '{}'".format(daemon)) return -1 # Connect tot the daemon. zmq_daemon = self._zmq_context.socket(zmq.PUSH) zmq_daemon.connect(self.__end_points[daemon]) # Send the halt message tot the daemon. message = HaltMessage() zmq_daemon.send_pyobj(message) # ----------------------------------------------------------------------------------------------------------------------
def __init__(self, address, context=None): self.context = context or zmq.Context() self.socket = self.context.socket(zmq.PUSH) self.socket.connect(address)
def test_context_manager(self): url = 'inproc://a' with self.Context() as ctx: with ctx.socket(zmq.PUSH) as a: a.bind(url) with ctx.socket(zmq.PULL) as b: b.connect(url) msg = b'hi' a.send(msg) rcvd = self.recv(b) self.assertEqual(rcvd, msg) self.assertEqual(b.closed, True) self.assertEqual(a.closed, True) self.assertEqual(ctx.closed, True)
def test_shadow(self): p = self.socket(zmq.PUSH) p.bind("tcp://127.0.0.1:5555") p2 = zmq.Socket.shadow(p.underlying) self.assertEqual(p.underlying, p2.underlying) s = self.socket(zmq.PULL) s2 = zmq.Socket.shadow(s.underlying) self.assertNotEqual(s.underlying, p.underlying) self.assertEqual(s.underlying, s2.underlying) s2.connect("tcp://127.0.0.1:5555") sent = b'hi' p2.send(sent) rcvd = self.recv(s2) self.assertEqual(rcvd, sent)
def test_recv_multipart(self): @gen.coroutine def test(): a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL) f = b.recv_multipart() assert not f.done() yield a.send(b'hi') recvd = yield f self.assertEqual(recvd, [b'hi']) self.loop.run_sync(test)
def test_recv(self): @gen.coroutine def test(): a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL) f1 = b.recv() f2 = b.recv() assert not f1.done() assert not f2.done() yield a.send_multipart([b'hi', b'there']) recvd = yield f2 assert f1.done() self.assertEqual(f1.result(), b'hi') self.assertEqual(recvd, b'there') self.loop.run_sync(test)
def test_recv_cancel(self): @gen.coroutine def test(): a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL) f1 = b.recv() f2 = b.recv_multipart() assert f1.cancel() assert f1.done() assert not f2.done() yield a.send_multipart([b'hi', b'there']) recvd = yield f2 assert f1.cancelled() assert f2.done() self.assertEqual(recvd, [b'hi', b'there']) self.loop.run_sync(test)
def test_retry_send(self): push = self.socket(zmq.PUSH) push.sndtimeo = self.timeout_ms self.alarm() self.assertRaises(zmq.Again, push.send, b('buf')) assert self.timer_fired
def test_proxy(self): if zmq.zmq_version_info() < (3,2): raise SkipTest("Proxies only in libzmq >= 3") dev = devices.ThreadProxy(zmq.PULL, zmq.PUSH, zmq.PUSH) binder = self.context.socket(zmq.REQ) iface = 'tcp://127.0.0.1' port = binder.bind_to_random_port(iface) port2 = binder.bind_to_random_port(iface) port3 = binder.bind_to_random_port(iface) binder.close() time.sleep(0.1) dev.bind_in("%s:%i" % (iface, port)) dev.bind_out("%s:%i" % (iface, port2)) dev.bind_mon("%s:%i" % (iface, port3)) dev.start() time.sleep(0.25) msg = b'hello' push = self.context.socket(zmq.PUSH) push.connect("%s:%i" % (iface, port)) pull = self.context.socket(zmq.PULL) pull.connect("%s:%i" % (iface, port2)) mon = self.context.socket(zmq.PULL) mon.connect("%s:%i" % (iface, port3)) push.send(msg) self.sockets.extend([push, pull, mon]) self.assertEqual(msg, self.recv(pull)) self.assertEqual(msg, self.recv(mon))