I have a use case where the gRPC client (in Java) needs to communicate with a gRPC server (in Python). The communication must be async full duplex. For example, the server must be able to initiate communication with the client in addition to the normal use case of the client initiating communication with the server using an RPC call. It must be async because there is no guarantee of ordering of requests or of responses, so we’re using a session id to match requests to responses. On the server side, I can use the context within the handler method, but once that method returns, the context is closed and no writing can occur. Plus, this approach requires the client to send the first message.
Java seems to have no problem with this, using a StreamObserver and the OnNext( ) method. All other languages that support gRPC directly support this, only Python does not, and that is intentional according to the Python team via a post on StackOverflow.
I’ve tried to mimic a StreamObserver in Python using async queues, and it seems to work well for the receive messages, but I haven’t been able to find where to “tap into” the stream for writing to the client. I’ve read the Python gRPC example projects, the online reference and tutorial docs, etc, but none seem to match my use case. (This is my first Python project so I don’t pretend to expertise.) I am able to create various proto files, generate the pb2 and grpc_pb2 files, and create client-server request-response proof-of-concept projects with no problem.
Can this be done in Python? Any suggestions? Two things I’m starting to try are (1) setting client keepalive and (2) repeatedly calling dir( ) on the services object until I find some sort of read and write methods. Seems there should be a better way. I considered trying to just set the context to ACTIVE but that wouldn’t work if the underlying TCP connection has been closed.