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 Project: westpa   Author: westpa   File: test_worker.py    License: 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 Project: vnpy_crypto   Author: birforce   File: test_monitor.py    License: 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 #3
Source Project: vnpy_crypto   Author: birforce   File: test_device.py    License: 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 #4
Source Project: lewis   Author: ess-dmsc   File: control_server.py    License: 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 #5
Source Project: testplan   Author: Morgan-Stanley   File: connection.py    License: 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 #6
Source Project: phillip   Author: vladfi1   File: memory_watcher.py    License: 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 #7
Source Project: pySINDy   Author: luckystarufo   File: test_monitor.py    License: 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 #8
Source Project: pySINDy   Author: luckystarufo   File: test_device.py    License: 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 #9
Source Project: cddd   Author: jrwnter   File: inference.py    License: 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 #10
Source Project: InplusTrader_Linux   Author: zhengwsh   File: vnrpc.py    License: 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 #11
Source Project: ansible-jupyter-kernel   Author: ansible   File: kernel.py    License: 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 #12
Source Project: dataflow   Author: tensorpack   File: parallel_map.py    License: Apache License 2.0 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 #13
Source Project: pyGSTi   Author: pyGSTio   File: mpinoseutils.py    License: Apache License 2.0 5 votes vote down vote up
def _mpi_worker(addr):
    import importlib
    import zmq
    from pickle import loads, dumps

    rank = MPI.COMM_WORLD.Get_rank()
    if rank == 0:
        zctx = zmq.Context()
        socket = zctx.socket(zmq.REP)
        socket.connect(addr)

    while True:
        if rank == 0:
            pickled_msg = socket.recv()
        else:
            pickled_msg = None
        pickled_msg = MPI.COMM_WORLD.bcast(pickled_msg, root=0)
        msg = loads(pickled_msg)
        if msg == 'stop':
            if rank == 0:
                socket.send_pyobj('')
            break
        else:
            module_name, func_name = msg
            mod = importlib.import_module(module_name)
            func = getattr(mod, func_name)
            status = func(_return_status=True)
            if rank == 0:
                socket.send_pyobj(status)
    # All processes wait until they can terminate
    MPI.COMM_WORLD.barrier() 
Example #14
Source Project: cloud-asr   Author: UFAL-DSG   File: lib.py    License: Apache License 2.0 5 votes vote down vote up
def create_poller(worker_address, frontend_address):
    context = zmq.Context()
    worker_socket = context.socket(zmq.PULL)
    worker_socket.bind(worker_address)
    frontend_socket = context.socket(zmq.REP)
    frontend_socket.bind(frontend_address)

    sockets = {
        "worker": {"socket": worker_socket, "receive": worker_socket.recv, "send": worker_socket.send_json},
        "frontend": {"socket": frontend_socket, "receive": frontend_socket.recv, "send": frontend_socket.send},
    }
    time_func = time.time

    return Poller(sockets, time_func) 
Example #15
Source Project: cloud-asr   Author: UFAL-DSG   File: lib.py    License: Apache License 2.0 5 votes vote down vote up
def create_poller(frontend_address):
    from cloudasr import Poller
    context = zmq.Context()
    frontend_socket = context.socket(zmq.REP)
    frontend_socket.bind(frontend_address)

    sockets = {
        "frontend": {"socket": frontend_socket, "receive": frontend_socket.recv, "send": frontend_socket.send},
    }
    time_func = time.time

    return Poller(sockets, time_func) 
Example #16
Source Project: vnpy_crypto   Author: birforce   File: base.py    License: 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 #17
Source Project: vnpy_crypto   Author: birforce   File: test_error.py    License: 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 #18
Source Project: vnpy_crypto   Author: birforce   File: test_error.py    License: 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 #19
Source Project: vnpy_crypto   Author: birforce   File: test_socket.py    License: 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 #20
Source Project: vnpy_crypto   Author: birforce   File: test_socket.py    License: 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 #21
Source Project: vnpy_crypto   Author: birforce   File: test_context.py    License: 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 Project: vnpy_crypto   Author: birforce   File: test_context.py    License: 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 #23
Source Project: vnpy_crypto   Author: birforce   File: test_context.py    License: 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 #24
Source Project: vnpy_crypto   Author: birforce   File: test_context.py    License: 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 #25
Source Project: vnpy_crypto   Author: birforce   File: test_ioloop.py    License: MIT License 5 votes vote down vote up
def test_close_all(self):
        """Test close(all_fds=True)"""
        loop = ioloop.IOLoop.instance()
        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 #26
Source Project: vnpy_crypto   Author: birforce   File: test_security.py    License: MIT License 5 votes vote down vote up
def zap_handler(self):
        socket = self.context.socket(zmq.REP)
        socket.bind("inproc://zeromq.zap.01")
        try:
            msg = self.recv_multipart(socket)

            version, sequence, domain, address, identity, mechanism = msg[:6]
            if mechanism == b'PLAIN':
                username, password = msg[6:]
            elif mechanism == b'CURVE':
                key = msg[6]

            self.assertEqual(version, b"1.0")
            self.assertEqual(identity, b"IDENT")
            reply = [version, sequence]
            if mechanism == b'CURVE' or \
                (mechanism == b'PLAIN' and username == USER and password == PASS) or \
                (mechanism == b'NULL'):
                reply.extend([
                    b"200",
                    b"OK",
                    b"anonymous",
                    b"\5Hello\0\0\0\5World",
                ])
            else:
                reply.extend([
                    b"400",
                    b"Invalid username or password",
                    b"",
                    b"",
                ])
            socket.send_multipart(reply)
        finally:
            socket.close() 
Example #27
Source Project: vnpy_crypto   Author: birforce   File: test_device.py    License: MIT License 5 votes vote down vote up
def test_single_socket_forwarder_bind(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)
        # select random port:
        binder = self.context.socket(zmq.REQ)
        port = binder.bind_to_random_port('tcp://127.0.0.1')
        binder.close()
        time.sleep(0.1)
        req = self.context.socket(zmq.REQ)
        req.connect('tcp://127.0.0.1:%i'%port)
        dev.bind_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)
        # select random port:
        binder = self.context.socket(zmq.REQ)
        port = binder.bind_to_random_port('tcp://127.0.0.1')
        binder.close()
        time.sleep(0.1)
        req = self.context.socket(zmq.REQ)
        req.connect('tcp://127.0.0.1:%i'%port)
        dev.bind_in('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 #28
Source Project: vnpy_crypto   Author: birforce   File: test_device.py    License: 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 #29
Source Project: vnpy_crypto   Author: birforce   File: test_reqrep.py    License: MIT License 5 votes vote down vote up
def test_multiple(self):
        s1, s2 = self.create_bound_pair(zmq.REQ, zmq.REP)

        for i in range(10):
            msg1 = i*b' '
            msg2 = self.ping_pong(s1, s2, msg1)
            self.assertEqual(msg1, msg2) 
Example #30
Source Project: vnpy_crypto   Author: birforce   File: test_reqrep.py    License: MIT License 5 votes vote down vote up
def test_bad_send_recv(self):
        s1, s2 = self.create_bound_pair(zmq.REQ, zmq.REP)
        
        if zmq.zmq_version() != '2.1.8':
            # this doesn't work on 2.1.8
            for copy in (True,False):
                self.assertRaisesErrno(zmq.EFSM, s1.recv, copy=copy)
                self.assertRaisesErrno(zmq.EFSM, s2.send, b'asdf', copy=copy)

        # I have to have this or we die on an Abort trap.
        msg1 = b'asdf'
        msg2 = self.ping_pong(s1, s2, msg1)
        self.assertEqual(msg1, msg2)