Python zmq.RCVTIMEO Examples

The following are 30 code examples of zmq.RCVTIMEO(). 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: __init__.py    From Bert-TextClassification with MIT License 6 votes vote down vote up
def server_status(self):
        """
            Get the current status of the server connected to this client

        :return: a dictionary contains the current status of the server connected to this client
        :rtype: dict[str, str]

        """
        try:
            self.receiver.setsockopt(zmq.RCVTIMEO, self.timeout)
            self._send(b'SHOW_CONFIG')
            return jsonapi.loads(self._recv().content[1])
        except zmq.error.Again as _e:
            t_e = TimeoutError(
                'no response from the server (with "timeout"=%d ms), '
                'is the server on-line? is network broken? are "port" and "port_out" correct?' % self.timeout)
            if _py2:
                raise t_e
            else:
                raise t_e from _e
        finally:
            self.receiver.setsockopt(zmq.RCVTIMEO, -1) 
Example #2
Source File: __init__.py    From Bert-THUCNews with MIT License 6 votes vote down vote up
def server_status(self):
        """
            Get the current status of the server connected to this client

        :return: a dictionary contains the current status of the server connected to this client
        :rtype: dict[str, str]

        """
        try:
            self.receiver.setsockopt(zmq.RCVTIMEO, self.timeout)
            self._send(b'SHOW_CONFIG')
            return jsonapi.loads(self._recv().content[1])
        except zmq.error.Again as _e:
            t_e = TimeoutError(
                'no response from the server (with "timeout"=%d ms), '
                'is the server on-line? is network broken? are "port" and "port_out" correct?' % self.timeout)
            if _py2:
                raise t_e
            else:
                raise t_e from _e
        finally:
            self.receiver.setsockopt(zmq.RCVTIMEO, -1) 
Example #3
Source File: jrpc_py.py    From TradeSim with Apache License 2.0 6 votes vote down vote up
def _do_connect(self):

        client_id = str(random.randint(1000000, 100000000))

        socket = self._ctx.socket(zmq.DEALER)
        identity = (client_id) + '$' + str(random.randint(1000000, 1000000000))
        identity = identity.encode('utf-8')
        socket.setsockopt(zmq.IDENTITY, identity)
        socket.setsockopt(zmq.RCVTIMEO, 500)
        socket.setsockopt(zmq.SNDTIMEO, 500)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._addr)

        return socket 
Example #4
Source File: test_supvisorszmq.py    From supvisors with Apache License 2.0 6 votes vote down vote up
def setUp(self):
        """ Create a dummy supvisors, ZMQ context and sockets. """
        from supvisors.supvisorszmq import (InternalEventPublisher,
                                            InternalEventSubscriber)
        # the dummy Supvisors is used for addresses and ports
        self.supvisors = MockedSupvisors()
        # create publisher and subscriber
        self.publisher = InternalEventPublisher(
            self.supvisors.address_mapper.local_address,
            self.supvisors.options.internal_port,
            self.supvisors.logger)
        self.subscriber = InternalEventSubscriber(
            self.supvisors.address_mapper.addresses,
            self.supvisors.options.internal_port)
        # socket configuration is meant to be blocking
        # however, a failure would block the unit test,
        # so a timeout is set for reception
        self.subscriber.socket.setsockopt(zmq.RCVTIMEO, 1000)
        # publisher does not wait for subscriber clients to work,
        # so give some time for connections
        time.sleep(1) 
Example #5
Source File: __init__.py    From emnlp19-moverscore with MIT License 6 votes vote down vote up
def _timeout(func):
        @wraps(func)
        def arg_wrapper(self, *args, **kwargs):
            if 'blocking' in kwargs and not kwargs['blocking']:
                # override client timeout setting if `func` is called in non-blocking way
                self.receiver.setsockopt(zmq.RCVTIMEO, -1)
            else:
                self.receiver.setsockopt(zmq.RCVTIMEO, self.timeout)
            try:
                return func(self, *args, **kwargs)
            except zmq.error.Again as _e:
                t_e = TimeoutError(
                    'no response from the server (with "timeout"=%d ms), please check the following:'
                    'is the server still online? is the network broken? are "port" and "port_out" correct? '
                    'are you encoding a huge amount of data whereas the timeout is too small for that?' % self.timeout)
                if _py2:
                    raise t_e
                else:
                    _raise(t_e, _e)
            finally:
                self.receiver.setsockopt(zmq.RCVTIMEO, -1)

        return arg_wrapper 
Example #6
Source File: handler.py    From huobi with MIT License 6 votes vote down vote up
def __init__(self, name, topic: (str, list) = None, *args, **kwargs):
        self.name = name
        self.topic = set(topic if isinstance(topic, list) else
                         [topic]) if topic is not None else set()
        self.ctx = zmq_ctx
        self.sub_socket = self.ctx.socket(zmq.SUB)
        self.sub_socket.setsockopt(zmq.RCVTIMEO, 3000)
        self._thread_pool = ThreadPoolExecutor(max_workers=1)
        self.inproc = set()

        if self.topic:  # 如果topic默认为None,则对所有的topic做处理
            for t in self.topic:
                self.sub_socket.setsockopt(zmq.SUBSCRIBE, pickle.dumps(t))
        else:
            self.sub_socket.subscribe('')

        if kwargs.get('latest', False):  # 可以通过latest(bool)来订阅最新的数据
            self.data_queue = deque(maxlen=1)
            self.latest = True
        else:
            self.data_queue = deque()
            self.latest = False
        self.__active = False 
Example #7
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 #8
Source File: cig_olt_zmq.py    From voltha with Apache License 2.0 6 votes vote down vote up
def __init__(self, ip_address, rx_incoming_queue):

        self.external_conn_tx = "tcp://{}:{}".format(ip_address, _OLTD_ZEROMQ_OMCI_PORT)
        self.socket_tx = zmq_context.socket(zmq.DEALER)
        self.socket_tx.connect(self.external_conn_tx)

        self.external_conn_rx = "tcp://{}:{}".format(ip_address, _OLTD_ZEROMQ_PUB_OMCI_PORT)
        self.socket_rx = zmq_context.socket(zmq.SUB)
        self.socket_rx.setsockopt(zmq.SUBSCRIBE,'')
        self.socket_rx.setsockopt(zmq.RCVTIMEO,1000)
        self.socket_rx.connect(self.external_conn_rx)
        #self.rx_callback = rx_callback
        self.rx_incoming_queue = rx_incoming_queue
        self.killflag = 0
        
        try:
            thread.start_new_thread(self.omci_receive,())
        except:
            log.exception(e.message) 
Example #9
Source File: cig_olt_zmq.py    From voltha with Apache License 2.0 6 votes vote down vote up
def __init__(self, ip_address, rx_incoming_queue):
        self.external_conn = "tcp://{}:{}".format(ip_address, _OLTD_ZEROMQ_PUB_OTHER_PORT)
        self.socket = zmq_context.socket(zmq.SUB)
        self.socket.setsockopt(zmq.SUBSCRIBE,'')
        self.socket.setsockopt(zmq.RCVHWM,10000)
        self.socket.setsockopt(zmq.RCVTIMEO,1000)
        self.socket.connect(self.external_conn)
        self.rx_incoming_queue = rx_incoming_queue
        self.killflag = 0
        
        log.info('CigZmqClientSub zmq.RCVHWM', self.socket.getsockopt(zmq.RCVHWM))
        
        try:
            thread.start_new_thread(self.sub_receive,())
        except:
            log.exception(e.message) 
Example #10
Source File: __init__.py    From bert-as-service with MIT License 6 votes vote down vote up
def _timeout(func):
        @wraps(func)
        def arg_wrapper(self, *args, **kwargs):
            if 'blocking' in kwargs and not kwargs['blocking']:
                # override client timeout setting if `func` is called in non-blocking way
                self.receiver.setsockopt(zmq.RCVTIMEO, -1)
            else:
                self.receiver.setsockopt(zmq.RCVTIMEO, self.timeout)
            try:
                return func(self, *args, **kwargs)
            except zmq.error.Again as _e:
                t_e = TimeoutError(
                    'no response from the server (with "timeout"=%d ms), please check the following:'
                    'is the server still online? is the network broken? are "port" and "port_out" correct? '
                    'are you encoding a huge amount of data whereas the timeout is too small for that?' % self.timeout)
                if _py2:
                    raise t_e
                else:
                    _raise(t_e, _e)
            finally:
                self.receiver.setsockopt(zmq.RCVTIMEO, -1)

        return arg_wrapper 
Example #11
Source File: cig_olt_zmq.py    From voltha with Apache License 2.0 5 votes vote down vote up
def sync_reconnect(self):
        self.socket.close()
        self.socket = zmq_context.socket(zmq.REQ)
        self.socket.setsockopt(zmq.RCVTIMEO,3000)
        self.socket.connect(self.external_conn) 
Example #12
Source File: cig_olt_zmq.py    From voltha with Apache License 2.0 5 votes vote down vote up
def __init__(self, ip_address, rx_incoming_queue):
        self.external_conn = "tcp://{}:{}".format(ip_address, _OLTD_ZEROMQ_SYNC_PORT)
        self.socket = zmq_context.socket(zmq.REQ)
        self.socket.setsockopt(zmq.RCVTIMEO,3000)
        self.socket.connect(self.external_conn)
        self.rx_incoming_queue = rx_incoming_queue
        self.response = None 
Example #13
Source File: rpc.py    From huobi with MIT License 5 votes vote down vote up
def __init__(self, reqAddr, subAddr, reqPort=6868, subPort=6869):
        self.__ctx = zmq.Context()
        self.__reqSocket = self.__ctx.socket(zmq.REQ)
        self.__subSocket = self.__ctx.socket(zmq.SUB)
        self.__subSocket.setsockopt(zmq.RCVTIMEO, 3000)
        self.__reqSocket.setsockopt(zmq.RCVTIMEO, 5000)
        self.__repAddr = f'tcp://{reqAddr}:{reqPort}'
        self.__subAddr = f'tcp://{subAddr}:{subPort}'
        self.__reqSocket.connect(self.__repAddr)
        self._active = False 
Example #14
Source File: cig_olt_zmq.py    From voltha with Apache License 2.0 5 votes vote down vote up
def __init__(self, ip_address, rx_callback):
       
        self.external_conn = "tcp://{}:{}".format(ip_address, _OLTD_ZEROMQ_ASYNC_PORT)
        self.socket = zmq_context.socket(zmq.DEALER)
        self.socket.setsockopt(zmq.RCVTIMEO,1000)
        self.socket.connect(self.external_conn)
        self.rx_callback = rx_callback
        #self.rx_incoming_queue = rx_incoming_queue
        self.killflag = 0
        
        try:
            thread.start_new_thread(self.async_receive,())
        except:
            log.exception(e.message) 
Example #15
Source File: jrpc_py.py    From DataApi with Apache License 2.0 5 votes vote down vote up
def _do_connect(self):

        client_id = str(random.randint(1000000, 100000000))

        socket = self._ctx.socket(zmq.DEALER)
        identity = (client_id) + '$' + str(random.randint(1000000, 1000000000))
        identity = identity.encode('utf-8')
        socket.setsockopt(zmq.IDENTITY, identity)
        socket.setsockopt(zmq.RCVTIMEO, 500)
        socket.setsockopt(zmq.SNDTIMEO, 500)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._addr)

        return socket 
Example #16
Source File: test_zeromq_client.py    From jsonrpcclient with MIT License 5 votes vote down vote up
def test_send_message_conn_error(self):
        client = ZeroMQClient("tcp://localhost:5555")
        # Set timeouts
        client.socket.setsockopt(zmq.RCVTIMEO, 5)
        client.socket.setsockopt(zmq.SNDTIMEO, 5)
        client.socket.setsockopt(zmq.LINGER, 5)
        with pytest.raises(zmq.error.ZMQError):
            client.send_message(str(Request("go")), response_expected=True) 
Example #17
Source File: jrpc_py.py    From TradeApi with Apache License 2.0 5 votes vote down vote up
def _do_connect(self):

        client_id = str(random.randint(1000000, 100000000))

        socket = self._ctx.socket(zmq.DEALER)
        identity = (client_id) + '$' + str(random.randint(1000000, 1000000000))
        identity = identity.encode('utf-8')
        socket.setsockopt(zmq.IDENTITY, identity)
        socket.setsockopt(zmq.RCVTIMEO, 500)
        socket.setsockopt(zmq.SNDTIMEO, 500)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._addr)

        return socket 
Example #18
Source File: tcpclient.py    From pyModeS with GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        self.socket = zmq.Context().socket(zmq.STREAM)
        self.socket.setsockopt(zmq.LINGER, 0)
        self.socket.setsockopt(zmq.RCVTIMEO, 10000)
        self.socket.connect("tcp://%s:%s" % (self.host, self.port)) 
Example #19
Source File: client.py    From cloudburst with Apache License 2.0 5 votes vote down vote up
def _connect(self):
        sckt = self.context.socket(zmq.REQ)
        sckt.setsockopt(zmq.RCVTIMEO, 1000)
        sckt.connect(self.service_addr % CONNECT_PORT)
        sckt.send_string('')

        try:
            result = sckt.recv_string()
            return result
        except zmq.ZMQError as e:
            if e.errno == zmq.EAGAIN:
                return None
            else:
                raise e 
Example #20
Source File: anna_ipc_client.py    From cloudburst with Apache License 2.0 5 votes vote down vote up
def __init__(self, thread_id=0, context=None):
        if not context:
            self.context = zmq.Context(1)
        else:
            self.context = context

        self.get_response_address = GET_RESPONSE_ADDR_TEMPLATE % thread_id
        self.put_response_address = PUT_RESPONSE_ADDR_TEMPLATE % thread_id

        self.get_request_socket = self.context.socket(zmq.PUSH)
        self.get_request_socket.connect(GET_REQUEST_ADDR)

        self.put_request_socket = self.context.socket(zmq.PUSH)
        self.put_request_socket.connect(PUT_REQUEST_ADDR)

        self.get_response_socket = self.context.socket(zmq.PULL)
        self.get_response_socket.setsockopt(zmq.RCVTIMEO, 5000)
        self.get_response_socket.bind(self.get_response_address)

        self.put_response_socket = self.context.socket(zmq.PULL)
        self.put_response_socket.setsockopt(zmq.RCVTIMEO, 5000)
        self.put_response_socket.bind(self.put_response_address)

        self.rid = 0

        # Set this to None because we do not use the address cache, but the
        # super class checks to see if there is one.
        self.address_cache = None 
Example #21
Source File: main.py    From gr-opssat with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent, ip, port):
		QtCore.QThread.__init__(self, parent)
		self.parent = parent
		self.active = True

		self.context = zmq.Context()
		self.socket = self.context.socket(zmq.SUB)
		self.host = 'tcp://' + ip + ':' + str(port)
		self.socket.connect(self.host)
		self.socket.setsockopt_string(zmq.SUBSCRIBE, "")
		self.socket.setsockopt(zmq.RCVTIMEO, 15000) #Timeout set to 15s, beacon frame is radiated every 10s by the spacecraft

		eventLogger.info('Started listening on {HOST} for incoming packets from GR flowgraph'.format(HOST=self.host)) 
Example #22
Source File: notifications.py    From cstar_perf with Apache License 2.0 5 votes vote down vote up
def zmq_socket_subscribe(url, topic='', timeout=5000):
    zmq_context = zmq.Context()
    zmq_socket = zmq_context.socket(zmq.SUB)
    zmq_socket.connect(url)
    zmq_socket.setsockopt_string(
        zmq.SUBSCRIBE, 
        unicode(topic))
    # Timeout:
    zmq_socket.setsockopt(zmq.RCVTIMEO, timeout)
    return zmq_socket 
Example #23
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 #24
Source File: client_api.py    From GaragePi with MIT License 5 votes vote down vote up
def __init__(self, logger: logging.Logger, connect_port='5550'):
        assert logger is not None
        self.__logger = logger

        self.__connect_addr = "tcp://localhost:%s" % connect_port
        self.__logger.debug("Connect address: " + self.__connect_addr)

        self.__context = zmq.Context()
        self.__context.setsockopt(zmq.RCVTIMEO, RECV_TIMEOUT)
        self.__poller = zmq.Poller()
        self.__socket = None    # type: zmq.sugar.Socket
        self.__create_socket() 
Example #25
Source File: test_supvisorszmq.py    From supvisors with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        """ Create a dummy supvisors and a ZMQ context. """
        from supvisors.supvisorszmq import EventPublisher, EventSubscriber
        # the dummy Supvisors is used for addresses and ports
        self.supvisors = MockedSupvisors()
        # create the ZeroMQ context
        # create publisher and subscriber
        self.publisher = EventPublisher(
            self.supvisors.options.event_port,
            self.supvisors.logger)
        self.subscriber = EventSubscriber(
            zmq.Context.instance(),
            self.supvisors.options.event_port,
            self.supvisors.logger)
        # WARN: this subscriber does not include a subscription
        # when using a subscription, use a time sleep to give time
        # to PyZMQ to handle it
        # WARN: socket configuration is meant to be blocking
        # however, a failure would block the unit test,
        # so a timeout is set for reception
        self.subscriber.socket.setsockopt(zmq.RCVTIMEO, 1000)
        # create test payloads
        self.supvisors_payload = Payload({'state': 'running',
                                          'version': '1.0'})
        self.address_payload = Payload({'state': 'silent',
                                        'name': 'cliche01',
                                        'date': 1234})
        self.application_payload = Payload({'state': 'starting',
                                            'name': 'supvisors'})
        self.process_payload = Payload({'state': 'running',
                                        'process_name': 'plugin',
                                        'application_name': 'supvisors',
                                        'date': 1230})
        self.event_payload = Payload({'state': 20,
                                      'name': 'plugin',
                                      'group': 'supvisors',
                                      'now': 1230}) 
Example #26
Source File: test_supvisorszmq.py    From supvisors with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        """ Create a dummy supvisors, ZMQ context and sockets. """
        from supvisors.supvisorszmq import RequestPusher, RequestPuller
        # the dummy Supvisors is used for addresses and ports
        self.supvisors = MockedSupvisors()
        # create pusher and puller
        self.pusher = RequestPusher(self.supvisors.logger)
        self.puller = RequestPuller()
        # socket configuration is meant to be blocking
        # however, a failure would block the unit test,
        # so a timeout is set for emission and reception
        self.puller.socket.setsockopt(zmq.SNDTIMEO, 1000)
        self.puller.socket.setsockopt(zmq.RCVTIMEO, 1000) 
Example #27
Source File: Client_Simple.py    From EDDN with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def main():
    context     = zmq.Context()
    subscriber  = context.socket(zmq.SUB)
    
    subscriber.setsockopt(zmq.SUBSCRIBE, b"")
    subscriber.setsockopt(zmq.RCVTIMEO, __timeoutEDDN)

    while True:
        try:
            subscriber.connect(__relayEDDN)
            
            while True:
                __message   = subscriber.recv()
                
                if __message == False:
                    subscriber.disconnect(__relayEDDN)
                    break
                
                __message   = zlib.decompress(__message)
                __json      = simplejson.loads(__message)
                
                # call dumps() to ensure double quotes in output
                print(simplejson.dumps(__json))
                sys.stdout.flush()
                
        except zmq.ZMQError as e:
            print ('ZMQSocketException: ' + str(e))
            sys.stdout.flush()
            subscriber.disconnect(__relayEDDN)
            time.sleep(5) 
Example #28
Source File: zeromq_queue.py    From plaso with Apache License 2.0 5 votes vote down vote up
def _SetSocketTimeouts(self):
    """Sets the timeouts for socket send and receive."""
    # Note that timeout must be an integer value. If timeout is a float
    # it appears that zmq will not enforce the timeout.
    timeout = int(self.timeout_seconds * 1000)
    receive_timeout = min(
        self._ZMQ_SOCKET_RECEIVE_TIMEOUT_MILLISECONDS, timeout)
    send_timeout = min(self._ZMQ_SOCKET_SEND_TIMEOUT_MILLISECONDS, timeout)

    self._zmq_socket.setsockopt(zmq.RCVTIMEO, receive_timeout)
    self._zmq_socket.setsockopt(zmq.SNDTIMEO, send_timeout) 
Example #29
Source File: test_control_server.py    From lewis with GNU General Public License v3.0 5 votes vote down vote up
def test_server_can_only_be_started_once(self, mock_context):
        server = ControlServer(None, connection_string='127.0.0.1:10000')
        server.start_server()
        server.start_server()

        mock_context.assert_has_calls([call(), call().socket(zmq.REP),
                                       call().socket().setsockopt(zmq.RCVTIMEO, 100),
                                       call().socket().bind('tcp://127.0.0.1:10000')]) 
Example #30
Source File: test_control_server.py    From lewis with GNU General Public License v3.0 5 votes vote down vote up
def test_connection(self, mock_context):
        cs = ControlServer(None, connection_string='127.0.0.1:10001')
        cs.start_server()

        mock_context.assert_has_calls([call(), call().socket(zmq.REP),
                                       call().socket().setsockopt(zmq.RCVTIMEO, 100),
                                       call().socket().bind('tcp://127.0.0.1:10001')])