@@ -52,10 +52,11 @@ def list_subscriptions_in_project(project_id):
52
52
subscriber = pubsub_v1 .SubscriberClient ()
53
53
project_path = subscriber .project_path (project_id )
54
54
55
- for subscription in subscriber .list_subscriptions (project_path ):
56
- print (subscription .name )
57
-
58
- subscriber .close ()
55
+ # Wrap the subscriber in a 'with' block to automatically call close() to
56
+ # close the underlying gRPC channel when done.
57
+ with subscriber :
58
+ for subscription in subscriber .list_subscriptions (project_path ):
59
+ print (subscription .name )
59
60
# [END pubsub_list_subscriptions]
60
61
61
62
@@ -73,11 +74,12 @@ def create_subscription(project_id, topic_id, subscription_id):
73
74
topic_path = subscriber .topic_path (project_id , topic_id )
74
75
subscription_path = subscriber .subscription_path (project_id , subscription_id )
75
76
76
- subscription = subscriber .create_subscription (subscription_path , topic_path )
77
+ # Wrap the subscriber in a 'with' block to automatically call close() to
78
+ # close the underlying gRPC channel when done.
79
+ with subscriber :
80
+ subscription = subscriber .create_subscription (subscription_path , topic_path )
77
81
78
82
print ("Subscription created: {}" .format (subscription ))
79
-
80
- subscriber .close ()
81
83
# [END pubsub_create_pull_subscription]
82
84
83
85
@@ -146,14 +148,15 @@ def create_push_subscription(project_id, topic_id, subscription_id, endpoint):
146
148
147
149
push_config = pubsub_v1 .types .PushConfig (push_endpoint = endpoint )
148
150
149
- subscription = subscriber .create_subscription (
150
- subscription_path , topic_path , push_config
151
- )
151
+ # Wrap the subscriber in a 'with' block to automatically call close() to
152
+ # close the underlying gRPC channel when done.
153
+ with subscriber :
154
+ subscription = subscriber .create_subscription (
155
+ subscription_path , topic_path , push_config
156
+ )
152
157
153
158
print ("Push subscription created: {}" .format (subscription ))
154
159
print ("Endpoint for subscription is: {}" .format (endpoint ))
155
-
156
- subscriber .close ()
157
160
# [END pubsub_create_push_subscription]
158
161
159
162
@@ -169,11 +172,12 @@ def delete_subscription(project_id, subscription_id):
169
172
subscriber = pubsub_v1 .SubscriberClient ()
170
173
subscription_path = subscriber .subscription_path (project_id , subscription_id )
171
174
172
- subscriber .delete_subscription (subscription_path )
175
+ # Wrap the subscriber in a 'with' block to automatically call close() to
176
+ # close the underlying gRPC channel when done.
177
+ with subscriber :
178
+ subscriber .delete_subscription (subscription_path )
173
179
174
180
print ("Subscription deleted: {}" .format (subscription_path ))
175
-
176
- subscriber .close ()
177
181
# [END pubsub_delete_subscription]
178
182
179
183
@@ -203,12 +207,13 @@ def update_push_subscription(project_id, topic_id, subscription_id, endpoint):
203
207
204
208
update_mask = {"paths" : {"push_config" }}
205
209
206
- result = subscriber .update_subscription (subscription , update_mask )
210
+ # Wrap the subscriber in a 'with' block to automatically call close() to
211
+ # close the underlying gRPC channel when done.
212
+ with subscriber :
213
+ result = subscriber .update_subscription (subscription , update_mask )
207
214
208
215
print ("Subscription updated: {}" .format (subscription_path ))
209
216
print ("New endpoint for subscription is: {}" .format (result .push_config ))
210
-
211
- subscriber .close ()
212
217
# [END pubsub_update_push_configuration]
213
218
214
219
@@ -436,24 +441,25 @@ def synchronous_pull(project_id, subscription_id):
436
441
437
442
NUM_MESSAGES = 3
438
443
439
- # The subscriber pulls a specific number of messages.
440
- response = subscriber .pull (subscription_path , max_messages = NUM_MESSAGES )
444
+ # Wrap the subscriber in a 'with' block to automatically call close() to
445
+ # close the underlying gRPC channel when done.
446
+ with subscriber :
447
+ # The subscriber pulls a specific number of messages.
448
+ response = subscriber .pull (subscription_path , max_messages = NUM_MESSAGES )
441
449
442
- ack_ids = []
443
- for received_message in response .received_messages :
444
- print ("Received: {}" .format (received_message .message .data ))
445
- ack_ids .append (received_message .ack_id )
450
+ ack_ids = []
451
+ for received_message in response .received_messages :
452
+ print ("Received: {}" .format (received_message .message .data ))
453
+ ack_ids .append (received_message .ack_id )
446
454
447
- # Acknowledges the received messages so they will not be sent again.
448
- subscriber .acknowledge (subscription_path , ack_ids )
455
+ # Acknowledges the received messages so they will not be sent again.
456
+ subscriber .acknowledge (subscription_path , ack_ids )
449
457
450
- print (
451
- "Received and acknowledged {} messages. Done." .format (
452
- len (response .received_messages )
458
+ print (
459
+ "Received and acknowledged {} messages. Done." .format (
460
+ len (response .received_messages )
461
+ )
453
462
)
454
- )
455
-
456
- subscriber .close ()
457
463
# [END pubsub_subscriber_sync_pull]
458
464
459
465
@@ -539,6 +545,8 @@ def worker(msg):
539
545
)
540
546
)
541
547
548
+ # Close the underlying gPRC channel. Alternatively, wrap subscriber in
549
+ # a 'with' block to automatically call close() when done.
542
550
subscriber .close ()
543
551
# [END pubsub_subscriber_sync_pull_with_lease]
544
552
0 commit comments