Python zmq.select() Examples

The following are 13 code examples of zmq.select(). 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: client.py    From indy-plenum with Apache License 2.0 6 votes vote down vote up
def check_tcp_connection(addr_port):
    sock = socket.socket()
    addr, port = addr_port.split(':')
    try:
        sock.connect((addr, int(port)))
        sock.send(TEST_MSG.encode())
    except OSError as e:
        print("TCP CHECK PHASE::: Cannot connect to {} because\n {}".format(addr_port, e))
        return False

    print("TCP CHECK PHASE:::Waiting {} seconds for response from server".format(WAIT_TIMEOUT))
    ready = select.select([sock], [], [], WAIT_TIMEOUT)
    if ready[0]:
        reply = sock.recv(1024)
        reply = reply.decode()
        print("TCP CHECK PHASE::: Got reply {}".format(reply))

        if reply != EXPECTED_TCP_REPLY:
            print("TCP CHECK PHASE:::TCP connection test failed. \nGot {} \nbut expected reply {}\n".format(reply, EXPECTED_TCP_REPLY))
        print("TCP CHECK PHASE:::Got expected response")
        return True
    return False 
Example #2
Source File: test_poll.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_pair(self):
        s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR)

        # Sleep to allow sockets to connect.
        wait()

        rlist, wlist, xlist = zmq.select([s1, s2], [s1, s2], [s1, s2])
        self.assert_(s1 in wlist)
        self.assert_(s2 in wlist)
        self.assert_(s1 not in rlist)
        self.assert_(s2 not in rlist) 
Example #3
Source File: test_poll.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def test_timeout(self):
        """make sure select timeout has the right units (seconds)."""
        s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        tic = time.time()
        r,w,x = zmq.select([s1,s2],[],[],.005)
        toc = time.time()
        self.assertTrue(toc-tic < 1)
        self.assertTrue(toc-tic > 0.001)
        tic = time.time()
        r,w,x = zmq.select([s1,s2],[],[],.25)
        toc = time.time()
        self.assertTrue(toc-tic < 1)
        self.assertTrue(toc-tic > 0.1) 
Example #4
Source File: __init__.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def _select_recv(self, multipart, socket, **kwargs):
        """call recv[_multipart] in a way that raises if there is nothing to receive"""
        if zmq.zmq_version_info() >= (3,1,0):
            # zmq 3.1 has a bug, where poll can return false positives,
            # so we wait a little bit just in case
            # See LIBZMQ-280 on JIRA
            time.sleep(0.1)
        
        r,w,x = zmq.select([socket], [], [], timeout=kwargs.pop('timeout', 5))
        assert len(r) > 0, "Should have received a message"
        kwargs['flags'] = zmq.DONTWAIT | kwargs.get('flags', 0)
        
        recv = socket.recv_multipart if multipart else socket.recv
        return recv(**kwargs) 
Example #5
Source File: client.py    From indy-plenum with Apache License 2.0 5 votes vote down vote up
def check_zmq_connection(addr_port):
    ctx = zmq.Context()
    sock = ctx.socket(zmq.DEALER)
    l_pub_key, l_sec_key = zmq.curve_keypair()
    sock.setsockopt(zmq.IDENTITY, base64.encodebytes(l_pub_key))
    sock.setsockopt(zmq.CURVE_PUBLICKEY, l_pub_key)
    sock.setsockopt(zmq.CURVE_SECRETKEY, l_sec_key)
    dest = get_dest(SERVER_SEED)
    sock.setsockopt(zmq.CURVE_SERVERKEY, z85.encode(ed_25519_pk_to_curve_25519(base58.b58decode(dest))))

    try:
        sock.connect("{}://{}".format(ZMQ_NETWORK_PROTOCOL, addr_port))
        sock.send_string(TEST_MSG)
    except OSError as e:
        print("ZMQ CHECK PHASE::: Cannot connect to {} because\n {}".format(addr_port, e))
        return False

    print("ZMQ CHECK PHASE:::Waiting {} seconds for response from server".format(WAIT_TIMEOUT))

    ready = zmq.select([sock], [], [], WAIT_TIMEOUT)
    if ready[0]:
        reply = sock.recv(flags=zmq.NOBLOCK)
        reply = reply.decode()
        print("ZMQ CHECK PHASE::: Got reply {}".format(reply))

        if reply != EXPECTED_ZMQ_REPLY:
            print("ZMQ CHECK PHASE:::ZMQ connection test failed. \nGot {} \nbut expected reply {}\n".format(reply, EXPECTED_ZMQ_REPLY))
        print("ZMQ CHECK PHASE:::Got expected response")
        return True
    return False 
Example #6
Source File: test_poll.py    From pySINDy with MIT License 5 votes vote down vote up
def test_pair(self):
        s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR)

        # Sleep to allow sockets to connect.
        wait()

        rlist, wlist, xlist = zmq.select([s1, s2], [s1, s2], [s1, s2])
        self.assert_(s1 in wlist)
        self.assert_(s2 in wlist)
        self.assert_(s1 not in rlist)
        self.assert_(s2 not in rlist) 
Example #7
Source File: test_poll.py    From pySINDy with MIT License 5 votes vote down vote up
def test_timeout(self):
        """make sure select timeout has the right units (seconds)."""
        s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        tic = time.time()
        r,w,x = zmq.select([s1,s2],[],[],.005)
        toc = time.time()
        self.assertTrue(toc-tic < 1)
        self.assertTrue(toc-tic > 0.001)
        tic = time.time()
        r,w,x = zmq.select([s1,s2],[],[],.25)
        toc = time.time()
        self.assertTrue(toc-tic < 1)
        self.assertTrue(toc-tic > 0.1) 
Example #8
Source File: __init__.py    From pySINDy with MIT License 5 votes vote down vote up
def _select_recv(self, multipart, socket, **kwargs):
        """call recv[_multipart] in a way that raises if there is nothing to receive"""
        if zmq.zmq_version_info() >= (3,1,0):
            # zmq 3.1 has a bug, where poll can return false positives,
            # so we wait a little bit just in case
            # See LIBZMQ-280 on JIRA
            time.sleep(0.1)
        
        r,w,x = zmq.select([socket], [], [], timeout=kwargs.pop('timeout', 5))
        assert len(r) > 0, "Should have received a message"
        kwargs['flags'] = zmq.DONTWAIT | kwargs.get('flags', 0)
        
        recv = socket.recv_multipart if multipart else socket.recv
        return recv(**kwargs) 
Example #9
Source File: ue4_image_bridge.py    From DroneSimLab with MIT License 5 votes vote down vote up
def listener():
    cnt=0
    while 1:
        while len(zmq.select([zmq_sub],[],[],0.001)[0])>0:
            topic, info, data = zmq_sub.recv_multipart()
            #topic=topic.decode()
            info=struct.unpack('llll',info)
            shape=info[:3]
            frame_cnt=info[3]
            img=np.fromstring(data,'uint8').reshape(shape)
            rgb=img[...,::-1]
            if cvshow:
                #if 'depth' in topic:
                #    cv2.imshow(topic,img)
                #else:
                #cv2.imshow(topic,cv2.resize(cv2.resize(img,(1920/2,1080/2)),(512,512)))
                img_shrk = img[::2,::2]
                cv2.imshow(topic.decode(),img)
                cv2.waitKey(1)
            if topic==topicm and gst:
                stdin.write(rgb.tostring())
            ### test
        time.sleep(0.010)
        if cnt%20==0:
            print('send...',cnt)
        if test:
            time.sleep(0.020)
            stdin.write(b'23\xff'*(sx*sy))
        cnt+=1 
Example #10
Source File: unreal_proxy.py    From DroneSimLab with MIT License 5 votes vote down vote up
def main_loop(gworld):
    drone_actor=ph.FindActorByName(gworld,'Parrot_Drone_6',1)
    #drone_camera_actor=ph.FindActorByName(gworld,'SceneCapture2Ddrone',1)
    if drone_actor is None:# or drone_camera_actor is None:
        print('ERROR: could not find drone_actor')
        while 1:
            yield
    for _ in range(10): #need to send it a few time don't know why.
        socket_pub.send_multipart([config.topic_unreal_state,b'main_loop'])
        yield
    drone_start_pos=np.array(ph.GetActorLocation(drone_actor))
    position=None
    while 1:
        while len(zmq.select([socket_sub],[],[],0)[0])>0:
            topic, msg = socket_sub.recv_multipart()
            position=pickle.loads(msg)
        if position is not None:
            new_pos=drone_start_pos+np.array([position['posx'],position['posy'],position['posz']])*100 #turn to cm
            ph.SetActorLocation(drone_actor,new_pos)
            ph.SetActorRotation(drone_actor,(position['roll'],position['pitch'],position['yaw']))
            #incase of gimabl
            #ph.SetActorRotation(drone_camera_actor,(-position['roll'],-position['pitch'],-position['yaw']))
            position=None
        yield
        img1=cv2.resize(ph.GetTextureImg(),(512,512),cv2.INTER_LINEAR)
        cv2.imshow('camera 1',img1)
        cv2.waitKey(1) 
Example #11
Source File: ue4_image_viewer.py    From DroneSimLab with MIT License 5 votes vote down vote up
def listener():
    while 1:
        while len(zmq.select([zmq_sub],[],[],0.001)[0])>0:
            topic, shape, data = zmq_sub.recv_multipart()
            topic=topic.decode()
            shape=struct.unpack('lll',shape)
            img=np.fromstring(data,'uint8').reshape(shape)
            if cvshow:
                if 'depth' in topic:
                    cv2.imshow(topic,img)
                else:
                    cv2.imshow(topic,cv2.resize(cv2.resize(img,(1024,1024)),(512,512)))
                cv2.waitKey(1) 
Example #12
Source File: client.py    From Computable with MIT License 4 votes vote down vote up
def _connect(self, sshserver, ssh_kwargs, timeout):
        """setup all our socket connections to the cluster. This is called from
        __init__."""

        # Maybe allow reconnecting?
        if self._connected:
            return
        self._connected=True

        def connect_socket(s, url):
            if self._ssh:
                return tunnel.tunnel_connection(s, url, sshserver, **ssh_kwargs)
            else:
                return s.connect(url)

        self.session.send(self._query_socket, 'connection_request')
        # use Poller because zmq.select has wrong units in pyzmq 2.1.7
        poller = zmq.Poller()
        poller.register(self._query_socket, zmq.POLLIN)
        # poll expects milliseconds, timeout is seconds
        evts = poller.poll(timeout*1000)
        if not evts:
            raise error.TimeoutError("Hub connection request timed out")
        idents,msg = self.session.recv(self._query_socket,mode=0)
        if self.debug:
            pprint(msg)
        content = msg['content']
        # self._config['registration'] = dict(content)
        cfg = self._config
        if content['status'] == 'ok':
            self._mux_socket = self._context.socket(zmq.DEALER)
            connect_socket(self._mux_socket, cfg['mux'])

            self._task_socket = self._context.socket(zmq.DEALER)
            connect_socket(self._task_socket, cfg['task'])

            self._notification_socket = self._context.socket(zmq.SUB)
            self._notification_socket.setsockopt(zmq.SUBSCRIBE, b'')
            connect_socket(self._notification_socket, cfg['notification'])

            self._control_socket = self._context.socket(zmq.DEALER)
            connect_socket(self._control_socket, cfg['control'])

            self._iopub_socket = self._context.socket(zmq.SUB)
            self._iopub_socket.setsockopt(zmq.SUBSCRIBE, b'')
            connect_socket(self._iopub_socket, cfg['iopub'])

            self._update_engines(dict(content['engines']))
        else:
            self._connected = False
            raise Exception("Failed to connect!")

    #--------------------------------------------------------------------------
    # handlers and callbacks for incoming messages
    #-------------------------------------------------------------------------- 
Example #13
Source File: client.py    From Computable with MIT License 4 votes vote down vote up
def resubmit(self, indices_or_msg_ids=None, metadata=None, block=None):
        """Resubmit one or more tasks.

        in-flight tasks may not be resubmitted.

        Parameters
        ----------

        indices_or_msg_ids : integer history index, str msg_id, or list of either
            The indices or msg_ids of indices to be retrieved

        block : bool
            Whether to wait for the result to be done

        Returns
        -------

        AsyncHubResult
            A subclass of AsyncResult that retrieves results from the Hub

        """
        block = self.block if block is None else block
        if indices_or_msg_ids is None:
            indices_or_msg_ids = -1

        if not isinstance(indices_or_msg_ids, (list,tuple)):
            indices_or_msg_ids = [indices_or_msg_ids]

        theids = []
        for id in indices_or_msg_ids:
            if isinstance(id, int):
                id = self.history[id]
            if not isinstance(id, basestring):
                raise TypeError("indices must be str or int, not %r"%id)
            theids.append(id)

        content = dict(msg_ids = theids)

        self.session.send(self._query_socket, 'resubmit_request', content)

        zmq.select([self._query_socket], [], [])
        idents,msg = self.session.recv(self._query_socket, zmq.NOBLOCK)
        if self.debug:
            pprint(msg)
        content = msg['content']
        if content['status'] != 'ok':
            raise self._unwrap_exception(content)
        mapping = content['resubmitted']
        new_ids = [ mapping[msg_id] for msg_id in theids ]

        ar = AsyncHubResult(self, msg_ids=new_ids)

        if block:
            ar.wait()

        return ar