L
L
LakeForest2022-02-26 23:05:27
Python
LakeForest, 2022-02-26 23:05:27

How to solve RuntimeWarning: coroutine 'TestService.Method' was never awaited (grpc.aio) error?

# сложная операция, занимающая время и cpu/gpu
def long_process(value):
    ...
    # time.sleep(2)
    result = ...
    return result

class TestService(test_pb2_grpc.SpeechToTextService):
    async def Method(self, request: Request, context: grpc.aio.ServicerContext):
        start = time.time()
        value= request.value
        ...
        result = await long_process(value)
        ...
        return Response(result = result, duration=time.time()-start)
        
async def main(address: str):
    server = grpc.aio.server(futures.ThreadPoolExecutor())
    test_pb2_grpc.add_TestServiceServicer_to_server(
        TestService(), server
    )
    server.add_insecure_port(address)
    await server.start()
    await server.wait_for_termination()

if __name__ == "__main__":
    asyncio.run(main("localhost:8000"))

When calling the method, an error occurs:

/usr/local/lib/python3.7/asyncio/events.py:88: RuntimeWarning: coroutine 'TestService.Method' was never awaited
backend_1 | self._context.run(self._callback, *self._args)

Can you tell me what is causing this error and how to fix it?
And will it be possible in this way to speed up the processing of the request so that the endpoint is not blocked on fastapi?

I tried to find a solution, they often show such an example and such . But I don't understand why mine is worse.

UPD: restarted the service again. It worked, but does not return the result of processing... The client does not receive the response result...
On the client:
@router.post("/test)
async def test(
        request: TestRequest
):
    response = await send_request(request.value)
    if not response:
        raise HTTPException('Ответ от grpc не был получен')
    return JSONResponse(
        content=response
    )

But if I write like this, then the error "coroutine 'TestService.Method' was never awaited" is returned again:
class TestService(test_pb2_grpc.SpeechToTextService):
    async def Method(self, request: Request, context: grpc.aio.ServicerContext):
        start = time.time()
        value= request.value
        ...
        result = await long_process(value)
        ...
        return Response(result = result.result(), duration=time.time()-start)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question