protocol buffers – How to keep bi-directional gRPC channel open from python client

I have defined a bi-dir gRPC streaming RPC for exchanging some configuration information with the server.

Server: Implemented in C++
Client: Implemented in python 3.10

The client opens channel with server using below:

As and when configuration is changed in the client, it builds the gRPC message and yields the message to server by calling the bi-dir RPC (RPC name “Push”). The client never explicitly closes the channel by calling close. But, as soon as server receives the message, the channel gets reset, so the next message from client uses another channel.

My question is what am I doing wrong in the client and why channel gets closed? One thing is that server actually doesn’t send anything back so if I have a for loop as below client code hangs:

responses = self.stub.Push(
for response in responses:"response is {}".format(response))

so I removed for loop and just printing responses, but the channel gets reset:

responses = self.stub.Push(
                  make_client_msg(msg),timeout=timeout)"response is {}".format(responses))

I hope I was able to explain the problem without getting too much into details.



I wrote a test asyncio client code, but when I run it, I get below error:

TypeError: object StreamStreamCall can't be used in 'await' expression

Below is my code:

async def run() -> None:
async with grpc.aio.insecure_channel('localhost:50052') as channel:
    stub = mygrpc_pb2.ServiceAPIStub(channel)
    response = await stub.Push(
print("Greeter client received: " + response.message)

I’m getting error at the line where I’m calling Push:

Below is how I’m calling run()

if __name__ == '__main__':
while True:

I’ve a global variable defined which make_msg keeps on appending with client message.

Read more here: Source link