【发布时间】:2021-03-11 03:30:12
【问题描述】:
我正在编写一个 GRPC python 客户端来与 GRPC 服务器进行通信,如下所示。
class GrpcClient:
"""
Class to send request to Grpc server.
"""
def __init__(self, host: str, port: int):
self._server_address = host + ':' + str(port)
with grpc.insecure_channel(self._server_address) as channel:
self._stub = MyServerStub(channel)
def invoke_method(self, request):
response = self._stub.process(request)
logging.info("response received: " + str(response))
这会导致错误 ValueError: Cannot invoke RPC on closed channel! 因为该通道在 int 上已关闭,我修改如下
def __init__(self, host: str, port: int):
self._server_address = host + ':' + str(port)
channel = grpc.insecure_channel(self._server_address)
self._stub = MyServerStub(channel)
现在通道和存根都被重用了,但我担心通道可能没有关闭,这可能导致内存泄漏
为了防止它发生,我尝试打开通道并在每个请求上创建存根,这很昂贵。
任何人都可以提出一种在没有任何泄漏的情况下重用通道和存根的方法吗?
【问题讨论】:
标签: python python-3.x memory-leaks grpc grpc-python