Python zmq.REP Examples

The following are 30 code examples of zmq.REP(). 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_worker.py    From westpa with MIT License 6 votes vote down vote up
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 #2
Source File: memory_watcher.py    From phillip with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, path=None, port=None, pull=False):
    try:
      import zmq
    except ImportError as err:
      print("ImportError: {0}".format(err))
      sys.exit("Need zmq installed.")

    self.pull = pull or port
    context = zmq.Context()
    self.socket = context.socket(zmq.PULL if self.pull else zmq.REP)
    if path:
      self.socket.bind("ipc://" + path)
    elif port:
      self.socket.bind("tcp://127.0.0.1:%d" % port)
    else:
      raise Exception("Must specify path or port.")
    
    self.messages = None 
Example #3
Source File: connection.py    From testplan with Apache License 2.0 6 votes vote down vote up
def starting(self):
        """Create a ZMQ context and socket to handle TCP communication."""
        if self.parent is None:
            raise RuntimeError("Parent pool was not set - cannot start.")

        self._zmq_context = zmq.Context()
        self._sock = self._zmq_context.socket(zmq.REP)
        if self.parent.cfg.port == 0:
            port_selected = self._sock.bind_to_random_port(
                "tcp://{}".format(self.parent.cfg.host)
            )
        else:
            self._sock.bind(
                "tcp://{}:{}".format(
                    self.parent.cfg.host, self.parent.cfg.port
                )
            )
            port_selected = self.parent.cfg.port
        self._address = "{}:{}".format(self.parent.cfg.host, port_selected)
        super(ZMQServer, self).starting() 
Example #4
Source File: kernel.py    From ansible-jupyter-kernel with Apache License 2.0 6 votes vote down vote up
def __init__(self, queue):
        self.queue = queue
        self.io_loop = IOLoop(make_current=False)
        context = zmq.Context.instance()
        self.pause_socket = context.socket(zmq.REP)
        self.pause_socket_port = self.pause_socket.bind_to_random_port(
            "tcp://127.0.0.1")
        self.status_socket = context.socket(zmq.PULL)
        self.status_socket_port = self.status_socket.bind_to_random_port(
            "tcp://127.0.0.1")

        self.pause_stream = ZMQStream(self.pause_socket, self.io_loop)
        self.status_stream = ZMQStream(self.status_socket, self.io_loop)

        self.pause_stream.on_recv(self.recv_pause)
        self.status_stream.on_recv(self.recv_status)
        self.thread = threading.Thread(target=self._thread_main)
        self.thread.daemon = True 
Example #5
Source File: test_device.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_single_socket_forwarder_connect(self):
        if zmq.zmq_version() in ('4.1.1', '4.0.6'):
            raise SkipTest("libzmq-%s broke single-socket devices" % zmq.zmq_version())
        dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
        req = self.context.socket(zmq.REQ)
        port = req.bind_to_random_port('tcp://127.0.0.1')
        dev.connect_in('tcp://127.0.0.1:%i'%port)
        dev.start()
        time.sleep(.25)
        msg = b'hello'
        req.send(msg)
        self.assertEqual(msg, self.recv(req))
        del dev
        req.close()
        dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
        req = self.context.socket(zmq.REQ)
        port = req.bind_to_random_port('tcp://127.0.0.1')
        dev.connect_out('tcp://127.0.0.1:%i'%port)
        dev.start()
        time.sleep(.25)
        msg = b'hello again'
        req.send(msg)
        self.assertEqual(msg, self.recv(req))
        del dev
        req.close() 
Example #6
Source File: vnrpc.py    From InplusTrader_Linux with MIT License 6 votes vote down vote up
def __init__(self, repAddress, pubAddress):
        """Constructor"""
        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 #7
Source File: inference.py    From cddd with MIT License 6 votes vote down vote up
def _init_server(self):
        infer_model = InferenceModel(
            model_dir=self.model_dir,
            gpu_mem_frac=self.gpu_mem_frac,
            use_gpu=True,
            batch_size=self.batch_size,
            beam_width=self.beam_width,
            maximum_iterations=self.maximum_iterations
        )
        context = zmq.Context()
        socket = context.socket(zmq.REP)
        socket.connect("tcp://localhost:%s" % self.port_backend)
        print("Server running on GPU ", os.environ['CUDA_VISIBLE_DEVICES'])
        while True:
            inp = json.loads(socket.recv())
            if inp[0]:
                embeddings = infer_model.seq_to_emb(inp[1])
                socket.send_string(json.dumps(embeddings.tolist()))
            else:
                smiles = infer_model.emb_to_seq(np.array(inp[1]))
                socket.send_string(json.dumps(smiles)) 
Example #8
Source File: test_monitor.py    From pySINDy with MIT License 6 votes vote down vote up
def test_monitor_connected(self):
        """Test connected monitoring socket."""
        s_rep = self.context.socket(zmq.REP)
        s_req = self.context.socket(zmq.REQ)
        self.sockets.extend([s_rep, s_req])
        s_req.bind("tcp://127.0.0.1:6667")
        # try monitoring the REP socket
        # create listening socket for monitor
        s_event = s_rep.get_monitor_socket()
        s_event.linger = 0
        self.sockets.append(s_event)
        # test receive event for connect event
        s_rep.connect("tcp://127.0.0.1:6667")
        m = recv_monitor_message(s_event)
        if m['event'] == zmq.EVENT_CONNECT_DELAYED:
            self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6667")
            # test receive event for connected event
            m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
        self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6667") 
Example #9
Source File: test_monitor.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_monitor_connected(self):
        """Test connected monitoring socket."""
        s_rep = self.context.socket(zmq.REP)
        s_req = self.context.socket(zmq.REQ)
        self.sockets.extend([s_rep, s_req])
        s_req.bind("tcp://127.0.0.1:6667")
        # try monitoring the REP socket
        # create listening socket for monitor
        s_event = s_rep.get_monitor_socket()
        s_event.linger = 0
        self.sockets.append(s_event)
        # test receive event for connect event
        s_rep.connect("tcp://127.0.0.1:6667")
        m = recv_monitor_message(s_event)
        if m['event'] == zmq.EVENT_CONNECT_DELAYED:
            self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6667")
            # test receive event for connected event
            m = recv_monitor_message(s_event)
        self.assertEqual(m['event'], zmq.EVENT_CONNECTED)
        self.assertEqual(m['endpoint'], b"tcp://127.0.0.1:6667") 
Example #10
Source File: test_device.py    From pySINDy with MIT License 6 votes vote down vote up
def test_single_socket_forwarder_connect(self):
        if zmq.zmq_version() in ('4.1.1', '4.0.6'):
            raise SkipTest("libzmq-%s broke single-socket devices" % zmq.zmq_version())
        dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
        req = self.context.socket(zmq.REQ)
        port = req.bind_to_random_port('tcp://127.0.0.1')
        dev.connect_in('tcp://127.0.0.1:%i'%port)
        dev.start()
        time.sleep(.25)
        msg = b'hello'
        req.send(msg)
        self.assertEqual(msg, self.recv(req))
        del dev
        req.close()
        dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
        req = self.context.socket(zmq.REQ)
        port = req.bind_to_random_port('tcp://127.0.0.1')
        dev.connect_out('tcp://127.0.0.1:%i'%port)
        dev.start()
        time.sleep(.25)
        msg = b'hello again'
        req.send(msg)
        self.assertEqual(msg, self.recv(req))
        del dev
        req.close() 
Example #11
Source File: control_server.py    From lewis with GNU General Public License v3.0 6 votes vote down vote up
def start_server(self):
        """
        Binds the server to the configured host and port and starts listening.
        """
        if self._socket is None:
            context = zmq.Context()
            self._socket = context.socket(zmq.REP)
            self._socket.setsockopt(zmq.RCVTIMEO, 100)
            self._socket.bind('tcp://{0}:{1}'.format(self.host, self.port))

            self.log.info('Listening on %s:%s', self.host, self.port) 
Example #12
Source File: test_context.py    From pySINDy with MIT License 5 votes vote down vote up
def test_destroy(self):
        """Context.destroy should close sockets"""
        ctx = self.Context()
        sockets = [ ctx.socket(zmq.REP) for i in range(65) ]
        
        # close half of the sockets
        [ s.close() for s in sockets[::2] ]
        
        ctx.destroy()
        # reaper is not instantaneous
        time.sleep(1e-2)
        for s in sockets:
            self.assertTrue(s.closed) 
Example #13
Source File: parallel_map.py    From ADL with MIT License 5 votes vote down vote up
def run(self):
            enable_death_signal(_warn=self.identity == b'0')
            ctx = zmq.Context()
            socket = ctx.socket(zmq.REP)
            socket.setsockopt(zmq.IDENTITY, self.identity)
            socket.set_hwm(self.hwm)
            socket.connect(self.pipename)

            while True:
                dp = loads(socket.recv(copy=False))
                dp = self.map_func(dp)
                socket.send(dumps(dp), copy=False) 
Example #14
Source File: test_context.py    From pySINDy with MIT License 5 votes vote down vote up
def test_destroy_linger(self):
        """Context.destroy should set linger on closing sockets"""
        req,rep = self.create_bound_pair(zmq.REQ, zmq.REP)
        req.send(b'hi')
        time.sleep(1e-2)
        self.context.destroy(linger=0)
        # reaper is not instantaneous
        time.sleep(1e-2)
        for s in (req,rep):
            self.assertTrue(s.closed) 
Example #15
Source File: test_context.py    From pySINDy with MIT License 5 votes vote down vote up
def test_term_thread(self):
        """ctx.term should not crash active threads (#139)"""
        ctx = self.Context()
        evt = Event()
        evt.clear()

        def block():
            s = ctx.socket(zmq.REP)
            s.bind_to_random_port('tcp://127.0.0.1')
            evt.set()
            try:
                s.recv()
            except zmq.ZMQError as e:
                self.assertEqual(e.errno, zmq.ETERM)
                return
            finally:
                s.close()
            self.fail("recv should have been interrupted with ETERM")
        t = Thread(target=block)
        t.start()
        
        evt.wait(1)
        self.assertTrue(evt.is_set(), "sync event never fired")
        time.sleep(0.01)
        ctx.term()
        t.join(timeout=1)
        self.assertFalse(t.is_alive(), "term should have interrupted s.recv()") 
Example #16
Source File: test_ioloop.py    From pySINDy with MIT License 5 votes vote down vote up
def test_close_all(self):
        """Test close(all_fds=True)"""
        loop = self.IOLoop.current()
        req,rep = self.create_bound_pair(zmq.REQ, zmq.REP)
        loop.add_handler(req, lambda msg: msg, ioloop.IOLoop.READ)
        loop.add_handler(rep, lambda msg: msg, ioloop.IOLoop.READ)
        self.assertEqual(req.closed, False)
        self.assertEqual(rep.closed, False)
        loop.close(all_fds=True)
        self.assertEqual(req.closed, True)
        self.assertEqual(rep.closed, True) 
Example #17
Source File: master_api.py    From zoe with Apache License 2.0 5 votes vote down vote up
def __init__(self, metrics: StatsManager, scheduler: ZoeBaseScheduler, state: SQLManager) -> None:
        self.context = zmq.Context()
        self.zmq_s = self.context.socket(zmq.REP)
        self.listen_uri = zoe_lib.config.get_conf().api_listen_uri
        self.zmq_s.bind(self.listen_uri)
        self.debug_has_replied = False
        self.metrics = metrics
        self.scheduler = scheduler
        self.state = state 
Example #18
Source File: p2p.py    From gateway with GNU Affero General Public License v3.0 5 votes vote down vote up
def _listen(self):
        self.log("init server %s" % self._uri)
        self._ctx = zmq.Context()
        self._socket = self._ctx.socket(zmq.REP)
        self._socket.bind(self._neturi)
        while True:
            try:
                message = self._socket.recv()
            except:
                message = None
            if message:
                self.on_raw_message(message)
                self._socket.send(json.dumps({'type': "ok"})) 
Example #19
Source File: parallel_map.py    From petridishnn with MIT License 5 votes vote down vote up
def run(self):
            enable_death_signal(_warn=self.identity == b'0')
            ctx = zmq.Context()
            socket = ctx.socket(zmq.REP)
            socket.setsockopt(zmq.IDENTITY, self.identity)
            socket.set_hwm(self.hwm)
            socket.connect(self.pipename)

            while True:
                dp = loads(socket.recv(copy=False))
                dp = self.map_func(dp)
                socket.send(dumps(dp), copy=False) 
Example #20
Source File: test_device.py    From pySINDy with MIT License 5 votes vote down vote up
def test_green_device(self):
            rep = self.context.socket(zmq.REP)
            req = self.context.socket(zmq.REQ)
            self.sockets.extend([req, rep])
            port = rep.bind_to_random_port('tcp://127.0.0.1')
            g = gevent.spawn(zmq.green.device, zmq.QUEUE, rep, rep)
            req.connect('tcp://127.0.0.1:%i' % port)
            req.send(b'hi')
            timeout = gevent.Timeout(3)
            timeout.start()
            receiver = gevent.spawn(req.recv)
            self.assertEqual(receiver.get(2), b'hi')
            timeout.cancel()
            g.kill(block=True) 
Example #21
Source File: test_context.py    From pySINDy with MIT License 5 votes vote down vote up
def test_many_sockets(self):
        """opening and closing many sockets shouldn't cause problems"""
        ctx = self.Context()
        for i in range(16):
            sockets = [ ctx.socket(zmq.REP) for i in range(65) ]
            [ s.close() for s in sockets ]
            # give the reaper a chance
            time.sleep(1e-2)
        ctx.term() 
Example #22
Source File: test_socket.py    From pySINDy with MIT License 5 votes vote down vote up
def test_close_after_destroy(self):
        """s.close() after ctx.destroy() should be fine"""
        ctx = self.Context()
        s = ctx.socket(zmq.REP)
        ctx.destroy()
        # reaper is not instantaneous
        time.sleep(1e-2)
        s.close()
        self.assertTrue(s.closed) 
Example #23
Source File: test_socket.py    From pySINDy with MIT License 5 votes vote down vote up
def test_subclass(self):
        """subclasses can assign attributes"""
        class S(zmq.Socket):
            a = None
            def __init__(self, *a, **kw):
                self.a=-1
                super(S, self).__init__(*a, **kw)
        
        s = S(self.context, zmq.REP)
        self.sockets.append(s)
        self.assertEqual(s.a, -1)
        s.a=1
        self.assertEqual(s.a, 1)
        a=s.a
        self.assertEqual(a, 1) 
Example #24
Source File: test_error.py    From pySINDy with MIT License 5 votes vote down vote up
def atest_ctxterm(self):
        s = self.context.socket(zmq.REP)
        t = Thread(target=self.context.term)
        t.start()
        self.assertRaises(ContextTerminated, s.recv, zmq.NOBLOCK)
        self.assertRaisesErrno(zmq.TERM, s.recv, zmq.NOBLOCK)
        s.close()
        t.join() 
Example #25
Source File: test_error.py    From pySINDy with MIT License 5 votes vote down vote up
def test_again(self):
        s = self.context.socket(zmq.REP)
        self.assertRaises(Again, s.recv, zmq.NOBLOCK)
        self.assertRaisesErrno(zmq.EAGAIN, s.recv, zmq.NOBLOCK)
        s.close() 
Example #26
Source File: base.py    From pySINDy with MIT License 5 votes vote down vote up
def start(self):
        """Create and bind the ZAP socket"""
        self.zap_socket = self.context.socket(zmq.REP)
        self.zap_socket.linger = 1
        self.zap_socket.bind("inproc://zeromq.zap.01")
        self.log.debug("Starting") 
Example #27
Source File: __init__.py    From stytra with GNU General Public License v3.0 5 votes vote down vote up
def run(self):
        self.zmq_context = zmq.Context()
        self.zmq_socket = self.zmq_context.socket(zmq.REP)
        self.zmq_socket.setsockopt(zmq.LINGER, 0)
        self.zmq_socket.bind("tcp://*:{}".format(self.port))
        self.zmq_socket.setsockopt(zmq.RCVTIMEO, -1)

        super().run() 
Example #28
Source File: learner.py    From rl_algorithms with MIT License 5 votes vote down vote up
def init_communication(self):
        """Initialize sockets for communication."""
        ctx = zmq.Context()
        # Socket to send updated network parameters to worker
        self.pub_socket = ctx.socket(zmq.PUB)
        self.pub_socket.setsockopt(zmq.SNDHWM, 2)
        self.pub_socket.bind(f"tcp://127.0.0.1:{self.comm_cfg.learner_worker_port}")

        # Socket to receive replay data and send new priorities to buffer
        self.rep_socket = ctx.socket(zmq.REP)
        self.rep_socket.bind(f"tcp://127.0.0.1:{self.comm_cfg.learner_buffer_port}")

        # Socket to send logging data to logger
        self.push_socket = ctx.socket(zmq.PUSH)
        self.push_socket.connect(f"tcp://127.0.0.1:{self.comm_cfg.learner_logger_port}") 
Example #29
Source File: base.py    From scarecrow with GNU General Public License v3.0 5 votes vote down vote up
def start_receiver(self, *args, **kwargs):
        """Starts the main reciver loop
        """
        logger.debug('Starting receiver thread for ZMQ in {}...'.format(
            self.__class__.__name__))
        context = zmq.Context()
        socket = context.socket(zmq.REP)
        logger.debug('Binding to {}'.format(self.send_server))
        socket.bind('tcp://*:{}'.format(self.send_port))
        while True:
            #  Wait for next request from client
            message = socket.recv()
            self.on_receive(message)
            self.process(message)
            self.send_ack(socket) 
Example #30
Source File: zeroless.py    From python-zeroless with GNU Lesser General Public License v2.1 5 votes vote down vote up
def reply(self):
        """
        Returns a callable and an iterable respectively. Those can be used to
        both transmit a message and/or iterate over incoming messages,
        that were requested by a request socket. Note that the iterable returns
        as many parts as sent by requesters. Also, the sender function has a
        ``print`` like signature, with an infinite number of arguments. Each one
        being a part of the complete message.

        :rtype: (function, generator)
        """
        sock = self.__sock(zmq.REP)
        return self.__send_function(sock), self.__recv_generator(sock)

    # Pair pattern