Beispiel #1
0
    async def _call_async(self, method_name: str, *args, **kwargs):
        """
        Sends a request to the socket and then wait for the reply.

        To deal with multiple, asynchronous requests we do not expect that the receive reply task
        scheduled from this call is the one that receives this call's reply and instead rely on
        Events to signal across multiple _async_call/_recv_reply tasks.
        """
        request = utils.rpc_request(method_name, *args, **kwargs)
        _log.debug("Sending request: %s", request)

        # setup an event to notify us when the reply is received (potentially by a task scheduled by
        # another call to _async_call). we do this before we send the request to catch the case
        # where the reply comes back before we re-enter this thread
        self._events[request.id] = asyncio.Event()

        # schedule a task to receive the reply to ensure we have a task to receive the reply
        asyncio.ensure_future(self._recv_reply())

        await self._async_socket.send_multipart([to_msgpack(request)])
        await self._events[request.id].wait()

        reply = self._replies.pop(request.id)
        if isinstance(reply, RPCError):
            raise utils.RPCError(reply.error)
        else:
            return reply.result
Beispiel #2
0
    async def _process_request(self, identity: bytes, empty_frame: list,
                               request: RPCRequest):
        """
        Executes the method specified in a JSON RPC request and then sends the reply to the socket.

        :param identity: Client identity provided by ZeroMQ
        :param empty_frame: Either an empty list or a single null frame depending on the client type
        :param request: JSON RPC request
        """
        try:
            _log.debug("Client %s sent request: %s", identity, request)
            start_time = datetime.now()
            reply = await self.rpc_spec.run_handler(request)
            if self.announce_timing:
                _log.info("Request {} for {} lasted {} seconds".format(
                    request.id, request.method,
                    (datetime.now() - start_time).total_seconds()))

            _log.debug("Sending client %s reply: %s", identity, reply)
            await self._socket.send_multipart(
                [identity, *empty_frame,
                 to_msgpack(reply)])
        except Exception as e:
            if self.serialize_exceptions:
                _log.exception('Exception thrown in _process_request')
            else:
                raise e
Beispiel #3
0
    def call(self,
             method_name: str,
             *args,
             rpc_timeout: float = None,
             **kwargs):
        """
        Send JSON RPC request to a backend socket and receive reply
        Note that this uses the default event loop to run in a blocking manner. If you would rather run in an async
        fashion or provide your own event loop then use .async_call instead

        :param method_name: Method name
        :param args: Args that will be passed to the remote function
        :param float rpc_timeout: Timeout in seconds for Server response, set to None to disable the timeout
        :param kwargs: Keyword args that will be passed to the remote function
        """
        request = utils.rpc_request(method_name, *args, **kwargs)
        _log.debug("Sending request: %s", request)

        self._socket.send_multipart([to_msgpack(request)])

        # if an rpc_timeout override is not specified, use the one set in the Client attributes
        if rpc_timeout is None:
            rpc_timeout = self.rpc_timeout

        start_time = time.time()
        while True:
            # Need to keep track of timeout manually in case this loop runs more than once. We subtract off already
            # elapsed time from the timeout. The call to max is to make sure we don't send a negative value
            # which would throw an error.
            timeout = max((start_time + rpc_timeout - time.time()) *
                          1000, 0) if rpc_timeout is not None else None
            if self._socket.poll(timeout) == 0:
                raise TimeoutError(
                    f"Timeout on client {self.endpoint}, method name {method_name}, class info: {self}"
                )

            raw_reply, = self._socket.recv_multipart()
            reply = from_msgpack(raw_reply)
            _log.debug("Received reply: %s", reply)

            # there's a possibility that the socket will have some leftover replies from a previous
            # request on it if that .call() was cancelled or timed out. Therefore, we need to discard replies that
            # don't match the request just like in the call_async case.
            if reply.id == request.id:
                break
            else:
                _log.debug('Discarding reply: %s', reply)

        for warning in reply.warnings:
            warn(f"{warning.kind}: {warning.body}")

        if isinstance(reply, RPCError):
            raise utils.RPCError(reply.error)
        else:
            return reply.result
Beispiel #4
0
def test_server(request, m_endpoints):
    context = zmq.Context()
    backend = context.socket(zmq.DEALER)
    backend.connect(m_endpoints[0])
    request.addfinalizer(backend.close)

    proc = Process(target=run_mock, args=m_endpoints)
    proc.start()

    request = rpc_request("add", 1, 2)
    backend.send(to_msgpack(request))
    reply = from_msgpack(backend.recv())
    assert reply.result == 3

    os.kill(proc.pid, signal.SIGINT)
Beispiel #5
0
    async def _process_request(self, identity: bytes, empty_frame: list,
                               request: RPCRequest):
        """
        Executes the method specified in a JSON RPC request and then sends the reply to the socket.

        :param identity: Client identity provided by ZeroMQ
        :param empty_frame: Either an empty list or a single null frame depending on the client type
        :param request: JSON RPC request
        """
        try:
            _log.debug("Client %s sent request: %s", identity, request)
            reply = await self.rpc_spec.run_handler(request)

            _log.debug("Sending client %s reply: %s", identity, reply)
            await self._socket.send_multipart(
                [identity, *empty_frame,
                 to_msgpack(reply)])
        except Exception:
            _log.exception('Exception thrown in _process_request')
Beispiel #6
0
from rpcq.messages import (RPCError)

log = logging.getLogger(__file__)


def test_messages():
    e = "Error"
    i = "asefa32423"
    m = RPCError(error=e, id=i)
    assert m.error == e
    assert m.id == i
    assert m.jsonrpc == "2.0"

    assert m['error'] == e
    assert m.get('error', 1) == e

    assert m.asdict() == {"error": e, "id": i, "jsonrpc": "2.0"}

    with pytest.raises(TypeError):
        RPCError(bad_field=1)


def test_max_xxx_len():
    obj = {
        f'q{n}': np.array([0.0 + 1.0j] * 100_000).tobytes(order='C')
        for n in range(16)
    }
    b = to_msgpack(obj)
    with pytest.raises(ValueError):
        from_msgpack(b, max_bin_len=2**20 - 1)