If I add_done_callback
to a grpc future object, did I need to close channel?
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)
If I use with-statement
to initialize channel, channel will close before callback.
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)
Which will raise error:
cription":"Channel closed!","file":"src/core/lib/surface/call.cc","file_line":727,"grpc_message":"Channel closed!","grpc_status":1}"