fastapi_websocket_rpc icon indicating copy to clipboard operation
fastapi_websocket_rpc copied to clipboard

Long-running RPC handlers block other RPC requests

Open sersorrel opened this issue 5 months ago • 3 comments

Say you have the following handler:

    async def takes_a_moment(self) -> str:
        await asyncio.sleep(1)
        return "hello world"

I adapted the test_recursive_rpc_calls test to see what would happen if I called this several times simultaneously (using asyncio.create_task to schedule all the calls for execution at once, rather than waiting for them to complete in order). I expected that while the handler is asyncio.sleeping, other handlers would be free to run, so regardless of how many times I call the handler, it shouldn't take much more than a second to complete (since that's the behaviour I would see if I were calling it like a regular async function, rather than via the RPC protocol):

@pytest.mark.asyncio
async def test_simultaneous_calls(server):
    async with WebSocketRpcClient(uri, RpcUtilityMethods(), default_response_timeout=10) as client:
        start = time.monotonic()
        t1 = asyncio.create_task(client.other.takes_a_moment())
        t2 = asyncio.create_task(client.other.takes_a_moment())
        t3 = asyncio.create_task(client.other.takes_a_moment())
        t4 = asyncio.create_task(client.other.takes_a_moment())
        t5 = asyncio.create_task(client.other.takes_a_moment())
        await asyncio.gather(t1, t2, t3, t4, t5)
        end = time.monotonic()
        # each request takes 1 second, and they should happen simultaneously,
        # so in total they should take only a little more than a second
        assert end - start < 2

However, this test fails:

FAILED tests/advanced_rpc_test.py::test_simultaneous_calls - assert (9187774.350943424 - 9187769.341026856) < 2

Rather than running simultaneously, the handlers are running one after the other – so the test takes five full seconds to complete...

$ echo "9187774.350943424 - 9187769.341026856" | bc
5.009916568

Is it expected behaviour that only one RPC handler can be executing at any given time?

sersorrel avatar Sep 24 '24 15:09 sersorrel