I have a service that needs to reuse/share a single Channel instance between multiple requests, often simultaneously. The service wraps all gRPC calls with a context manager, and it sets its channel to None upon __aexit__. This results in calling Channel's __del__ method, and closing the connection. This is causing a race condition between simultaneous requests because the channel connection is closed while some of these requests are still using it.
Do you have any suggestions to resolve this issue wisely?
You can create Channel once when your service starts and close it when your service shuts down. Underlying HTTP/2 connection will be established during first outgoing request and reconnections are automatic.
I have a service that needs to reuse/share a single
Channel
instance between multiple requests, often simultaneously. The service wraps all gRPC calls with a context manager, and it sets itschannel
toNone
upon__aexit__
. This results in callingChannel
's__del__
method, and closing the connection. This is causing a race condition between simultaneous requests because the channel connection is closed while some of these requests are still using it.Do you have any suggestions to resolve this issue wisely?