Answer the question
In order to leave comments, you need to log in
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"))
/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)
@router.post("/test)
async def test(
request: TestRequest
):
response = await send_request(request.value)
if not response:
raise HTTPException('Ответ от grpc не был получен')
return JSONResponse(
content=response
)
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 questionAsk a Question
731 491 924 answers to any question