Skip to content

Commit 545343e

Browse files
Merge pull request grpc#15725 from nathanielmanistaatgoogle/15537
Close channels in examples.
2 parents 33b77ee + 8234d14 commit 545343e

File tree

5 files changed

+52
-35
lines changed

5 files changed

+52
-35
lines changed

examples/python/helloworld/greeter_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222

2323

2424
def run():
25-
channel = grpc.insecure_channel('localhost:50051')
26-
stub = helloworld_pb2_grpc.GreeterStub(channel)
27-
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
25+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
26+
# used in circumstances in which the with statement does not fit the needs
27+
# of the code.
28+
with grpc.insecure_channel('localhost:50051') as channel:
29+
stub = helloworld_pb2_grpc.GreeterStub(channel)
30+
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
2831
print("Greeter client received: " + response.message)
2932

3033

examples/python/interceptors/default_value/greeter_client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ def run():
2727
message='Hello from your local interceptor!')
2828
default_value_interceptor = default_value_client_interceptor.DefaultValueClientInterceptor(
2929
default_value)
30-
channel = grpc.insecure_channel('localhost:50051')
31-
channel = grpc.intercept_channel(channel, default_value_interceptor)
32-
stub = helloworld_pb2_grpc.GreeterStub(channel)
33-
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
30+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
31+
# used in circumstances in which the with statement does not fit the needs
32+
# of the code.
33+
with grpc.insecure_channel('localhost:50051') as channel:
34+
intercept_channel = grpc.intercept_channel(channel,
35+
default_value_interceptor)
36+
stub = helloworld_pb2_grpc.GreeterStub(intercept_channel)
37+
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
3438
print("Greeter client received: " + response.message)
3539

3640

examples/python/interceptors/headers/greeter_client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
def run():
2626
header_adder_interceptor = header_manipulator_client_interceptor.header_adder_interceptor(
2727
'one-time-password', '42')
28-
channel = grpc.insecure_channel('localhost:50051')
29-
channel = grpc.intercept_channel(channel, header_adder_interceptor)
30-
stub = helloworld_pb2_grpc.GreeterStub(channel)
31-
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
28+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
29+
# used in circumstances in which the with statement does not fit the needs
30+
# of the code.
31+
with grpc.insecure_channel('localhost:50051') as channel:
32+
intercept_channel = grpc.intercept_channel(channel,
33+
header_adder_interceptor)
34+
stub = helloworld_pb2_grpc.GreeterStub(intercept_channel)
35+
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
3236
print("Greeter client received: " + response.message)
3337

3438

examples/python/multiplex/multiplex_client.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,23 @@ def guide_route_chat(route_guide_stub):
106106

107107

108108
def run():
109-
channel = grpc.insecure_channel('localhost:50051')
110-
greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
111-
route_guide_stub = route_guide_pb2_grpc.RouteGuideStub(channel)
112-
greeter_response = greeter_stub.SayHello(
113-
helloworld_pb2.HelloRequest(name='you'))
114-
print("Greeter client received: " + greeter_response.message)
115-
print("-------------- GetFeature --------------")
116-
guide_get_feature(route_guide_stub)
117-
print("-------------- ListFeatures --------------")
118-
guide_list_features(route_guide_stub)
119-
print("-------------- RecordRoute --------------")
120-
guide_record_route(route_guide_stub)
121-
print("-------------- RouteChat --------------")
122-
guide_route_chat(route_guide_stub)
109+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
110+
# used in circumstances in which the with statement does not fit the needs
111+
# of the code.
112+
with grpc.insecure_channel('localhost:50051') as channel:
113+
greeter_stub = helloworld_pb2_grpc.GreeterStub(channel)
114+
route_guide_stub = route_guide_pb2_grpc.RouteGuideStub(channel)
115+
greeter_response = greeter_stub.SayHello(
116+
helloworld_pb2.HelloRequest(name='you'))
117+
print("Greeter client received: " + greeter_response.message)
118+
print("-------------- GetFeature --------------")
119+
guide_get_feature(route_guide_stub)
120+
print("-------------- ListFeatures --------------")
121+
guide_list_features(route_guide_stub)
122+
print("-------------- RecordRoute --------------")
123+
guide_record_route(route_guide_stub)
124+
print("-------------- RouteChat --------------")
125+
guide_route_chat(route_guide_stub)
123126

124127

125128
if __name__ == '__main__':

examples/python/route_guide/route_guide_client.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,19 @@ def guide_route_chat(stub):
100100

101101

102102
def run():
103-
channel = grpc.insecure_channel('localhost:50051')
104-
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
105-
print("-------------- GetFeature --------------")
106-
guide_get_feature(stub)
107-
print("-------------- ListFeatures --------------")
108-
guide_list_features(stub)
109-
print("-------------- RecordRoute --------------")
110-
guide_record_route(stub)
111-
print("-------------- RouteChat --------------")
112-
guide_route_chat(stub)
103+
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
104+
# used in circumstances in which the with statement does not fit the needs
105+
# of the code.
106+
with grpc.insecure_channel('localhost:50051') as channel:
107+
stub = route_guide_pb2_grpc.RouteGuideStub(channel)
108+
print("-------------- GetFeature --------------")
109+
guide_get_feature(stub)
110+
print("-------------- ListFeatures --------------")
111+
guide_list_features(stub)
112+
print("-------------- RecordRoute --------------")
113+
guide_record_route(stub)
114+
print("-------------- RouteChat --------------")
115+
guide_route_chat(stub)
113116

114117

115118
if __name__ == '__main__':

0 commit comments

Comments
 (0)