From 8234d14113f484c60ee8633dc060f8054adf3748 Mon Sep 17 00:00:00 2001 From: Nathaniel Manista Date: Tue, 12 Jun 2018 17:30:23 +0000 Subject: Close channels in examples --- examples/python/helloworld/greeter_client.py | 9 ++++--- .../interceptors/default_value/greeter_client.py | 12 ++++++--- .../python/interceptors/headers/greeter_client.py | 12 ++++++--- examples/python/multiplex/multiplex_client.py | 31 ++++++++++++---------- examples/python/route_guide/route_guide_client.py | 23 +++++++++------- 5 files changed, 52 insertions(+), 35 deletions(-) (limited to 'examples/python') diff --git a/examples/python/helloworld/greeter_client.py b/examples/python/helloworld/greeter_client.py index a0aeb47bd7..24b49ac233 100644 --- a/examples/python/helloworld/greeter_client.py +++ b/examples/python/helloworld/greeter_client.py @@ -22,9 +22,12 @@ import helloworld_pb2_grpc def run(): - channel = grpc.insecure_channel('localhost:50051') - 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: + stub = helloworld_pb2_grpc.GreeterStub(channel) + response = stub.SayHello(helloworld_pb2.HelloRequest(name='you')) print("Greeter client received: " + response.message) 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) diff --git a/examples/python/multiplex/multiplex_client.py b/examples/python/multiplex/multiplex_client.py index 9baa10247d..19d39ce66e 100644 --- a/examples/python/multiplex/multiplex_client.py +++ b/examples/python/multiplex/multiplex_client.py @@ -106,20 +106,23 @@ def guide_route_chat(route_guide_stub): def run(): - channel = grpc.insecure_channel('localhost:50051') - greeter_stub = helloworld_pb2_grpc.GreeterStub(channel) - route_guide_stub = route_guide_pb2_grpc.RouteGuideStub(channel) - greeter_response = greeter_stub.SayHello( - helloworld_pb2.HelloRequest(name='you')) - print("Greeter client received: " + greeter_response.message) - print("-------------- GetFeature --------------") - guide_get_feature(route_guide_stub) - print("-------------- ListFeatures --------------") - guide_list_features(route_guide_stub) - print("-------------- RecordRoute --------------") - guide_record_route(route_guide_stub) - print("-------------- RouteChat --------------") - guide_route_chat(route_guide_stub) + # 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: + greeter_stub = helloworld_pb2_grpc.GreeterStub(channel) + route_guide_stub = route_guide_pb2_grpc.RouteGuideStub(channel) + greeter_response = greeter_stub.SayHello( + helloworld_pb2.HelloRequest(name='you')) + print("Greeter client received: " + greeter_response.message) + print("-------------- GetFeature --------------") + guide_get_feature(route_guide_stub) + print("-------------- ListFeatures --------------") + guide_list_features(route_guide_stub) + print("-------------- RecordRoute --------------") + guide_record_route(route_guide_stub) + print("-------------- RouteChat --------------") + guide_route_chat(route_guide_stub) if __name__ == '__main__': diff --git a/examples/python/route_guide/route_guide_client.py b/examples/python/route_guide/route_guide_client.py index f2d4317ef1..b4ff3239ba 100644 --- a/examples/python/route_guide/route_guide_client.py +++ b/examples/python/route_guide/route_guide_client.py @@ -100,16 +100,19 @@ def guide_route_chat(stub): def run(): - channel = grpc.insecure_channel('localhost:50051') - stub = route_guide_pb2_grpc.RouteGuideStub(channel) - print("-------------- GetFeature --------------") - guide_get_feature(stub) - print("-------------- ListFeatures --------------") - guide_list_features(stub) - print("-------------- RecordRoute --------------") - guide_record_route(stub) - print("-------------- RouteChat --------------") - guide_route_chat(stub) + # 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: + stub = route_guide_pb2_grpc.RouteGuideStub(channel) + print("-------------- GetFeature --------------") + guide_get_feature(stub) + print("-------------- ListFeatures --------------") + guide_list_features(stub) + print("-------------- RecordRoute --------------") + guide_record_route(stub) + print("-------------- RouteChat --------------") + guide_route_chat(stub) if __name__ == '__main__': -- cgit v1.2.3