diff options
author | Nathaniel Manista <nathaniel@google.com> | 2018-06-12 17:30:23 +0000 |
---|---|---|
committer | Nathaniel Manista <nathaniel@google.com> | 2018-06-14 17:16:04 +0000 |
commit | 8234d14113f484c60ee8633dc060f8054adf3748 (patch) | |
tree | 115e9e3d56178687028127a7d2dd457608e3ad54 /examples/python/interceptors | |
parent | a70053690a3f07ecf69ff283c288d018d2362ee4 (diff) |
Close channels in examples
Diffstat (limited to 'examples/python/interceptors')
-rw-r--r-- | examples/python/interceptors/default_value/greeter_client.py | 12 | ||||
-rw-r--r-- | examples/python/interceptors/headers/greeter_client.py | 12 |
2 files changed, 16 insertions, 8 deletions
diff --git a/examples/python/interceptors/default_value/greeter_client.py b/examples/python/interceptors/default_value/greeter_client.py index aba7571d83..da21ac68ec 100644 --- a/examples/python/interceptors/default_value/greeter_client.py +++ b/examples/python/interceptors/default_value/greeter_client.py @@ -27,10 +27,14 @@ def run(): message='Hello from your local interceptor!') default_value_interceptor = default_value_client_interceptor.DefaultValueClientInterceptor( default_value) - channel = grpc.insecure_channel('localhost:50051') - channel = grpc.intercept_channel(channel, default_value_interceptor) - stub = helloworld_pb2_grpc.GreeterStub(channel) - response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) + # NOTE(gRPC Python Team): .close() is possible on a channel and should be + # used in circumstances in which the with statement does not fit the needs + # of the code. + with grpc.insecure_channel('localhost:50051') as channel: + intercept_channel = grpc.intercept_channel(channel, + default_value_interceptor) + stub = helloworld_pb2_grpc.GreeterStub(intercept_channel) + response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message) diff --git a/examples/python/interceptors/headers/greeter_client.py b/examples/python/interceptors/headers/greeter_client.py index 2b0dd3e177..6a09a3b9c5 100644 --- a/examples/python/interceptors/headers/greeter_client.py +++ b/examples/python/interceptors/headers/greeter_client.py @@ -25,10 +25,14 @@ import header_manipulator_client_interceptor def run(): header_adder_interceptor = header_manipulator_client_interceptor.header_adder_interceptor( 'one-time-password', '42') - channel = grpc.insecure_channel('localhost:50051') - channel = grpc.intercept_channel(channel, header_adder_interceptor) - stub = helloworld_pb2_grpc.GreeterStub(channel) - response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) + # NOTE(gRPC Python Team): .close() is possible on a channel and should be + # used in circumstances in which the with statement does not fit the needs + # of the code. + with grpc.insecure_channel('localhost:50051') as channel: + intercept_channel = grpc.intercept_channel(channel, + header_adder_interceptor) + stub = helloworld_pb2_grpc.GreeterStub(intercept_channel) + response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message) |