Hi,
Trying to understand the ability for gRPC to use async/await available in python 3.6. Right now I'm modifying the grpc tutorial code to try to understand how async will work.
I'm using the route_guide examples and have modified the code slightly.
Is there a gRPC equivalent for async for ? Right now, this returns the error: "TypeError: 'async for' requires an object with __aiter__ method, got _Rendezvous" , which makes sense. But is there a way around this so that I don't need to block while waiting for responses?
Looked through the github issues, and it seems
https://github.com/grpc/grpc/issues/6046 talks about solutions to this, but the solution is to spawn another thread? Can async not be used with gRPC without needing threads?
async def guide_route_chat(stub):
responses = stub.RouteChat(generate_messages())
await asyncio.sleep(1)
async for response in responses:
print("Received message %s at %s" % (response.message, response.location))
async def tasks(num_tasks):
for elem in range(num_tasks):
print ("task %d" %(elem))
await asyncio.sleep(1.0)
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
asyncio.ensure_future(guide_route_chat(stub))
loop = asyncio.get_event_loop()
loop.run_until_complete(tasks(10))