0

I am a newbie to python async GRPC and I came up with this to handle an async stream:

async with grpc.aio.insecure_channel(target='localhost:6565', options=CHANNEL_OPTIONS) as channel:
            stub = get_results_grpc.ResultServiceStub(channel)
            stream = stub.GetResults(get_results_pb2.PositionsRequest(name=["test"]), timeout=10, metadata=metadata)
            try:
                async for resp in stream.__aiter__():
                    print(resp)
            except grpc.RpcError as e:
                print(e)

            await asyncio.sleep(10)
            stream.cancel()

However, once there is no result on in the steam, then loop will finish and then both the steam will be closed and the program exits. How could I keep the stream open ?

In Java, I could use a countdownLatch to keep async grpc stream open.

Guifan Li
  • 165
  • 2
  • 14
  • 1
    The timeout will expire 10s from when you initially make the call, regardless of data coming through or not. If you want to keep it open, then don't use a timeout. – alkasm Jan 04 '21 at 20:14
  • @alkasm that's very helpful, thanks a lot. – Guifan Li Jan 07 '21 at 18:11

0 Answers0