如果我add_done_callback一个grpc的未来对象,我需要关闭通道吗?
class CallBack(object):
def __init__(self,channel=None) -> None:
self._channel = channel
def __call__(self, future):
print(self.future.result())
# self._channel.close()
def test():
channel = grpc.insecure_channel('localhost:50051')
stub = helloworld_pb2_grpc.GreeterStub(channel)
call_future = stub.SayHello.future(helloworld_pb2.HelloRequest(name='you'))
call_back = CallBack(channel)
call_future.add_done_callback(call_back)如果使用with-statement初始化通道,通道会在回调前关闭。
def test():
with grpc.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
call_future = stub.SayHello.future(helloworld_pb2.HelloRequest(name='you'))
call_back = CallBack(channel)
call_future.add_done_callback(call_back)这将引发错误:
cription":"Channel closed!","file":"src/core/lib/surface/call.cc","file_line":727,"grpc_message":"Channel closed!","grpc_status":1}"发布于 2021-05-20 02:38:39
通常,您需要在某些时候关闭通道。另一种方法是随着时间的推移在进程中增加更多的通道,从而导致每个通道的内存泄漏。使用with语句的第二个示例中的问题是通道在回调有机会触发之前就关闭了。
如果执行回调时不再需要通道,那么您应该从该回调关闭通道,或者设置一点状态来指示另一个线程应该关闭通道。
https://stackoverflow.com/questions/67581097
复制相似问题