vmagamedov / grpclib

Pure-Python gRPC implementation for asyncio
http://grpclib.readthedocs.io
BSD 3-Clause "New" or "Revised" License
936 stars 92 forks source link

What is the best way to share a Channel between multiple gRPC requests? #61

Closed hyunilmmt closed 5 years ago

hyunilmmt commented 5 years ago

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?

vmagamedov commented 5 years ago

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.