@@ -52,6 +52,8 @@ def list_subscriptions_in_project(project_id):
52
52
53
53
for subscription in subscriber .list_subscriptions (project_path ):
54
54
print (subscription .name )
55
+
56
+ subscriber .close ()
55
57
# [END pubsub_list_subscriptions]
56
58
57
59
@@ -75,6 +77,8 @@ def create_subscription(project_id, topic_name, subscription_name):
75
77
)
76
78
77
79
print ("Subscription created: {}" .format (subscription ))
80
+
81
+ subscriber .close ()
78
82
# [END pubsub_create_pull_subscription]
79
83
80
84
@@ -104,6 +108,8 @@ def create_push_subscription(
104
108
105
109
print ("Push subscription created: {}" .format (subscription ))
106
110
print ("Endpoint for subscription is: {}" .format (endpoint ))
111
+
112
+ subscriber .close ()
107
113
# [END pubsub_create_push_subscription]
108
114
109
115
@@ -123,6 +129,8 @@ def delete_subscription(project_id, subscription_name):
123
129
subscriber .delete_subscription (subscription_path )
124
130
125
131
print ("Subscription deleted: {}" .format (subscription_path ))
132
+
133
+ subscriber .close ()
126
134
# [END pubsub_delete_subscription]
127
135
128
136
@@ -158,6 +166,8 @@ def update_subscription(project_id, subscription_name, endpoint):
158
166
159
167
print ("Subscription updated: {}" .format (subscription_path ))
160
168
print ("New endpoint for subscription is: {}" .format (result .push_config ))
169
+
170
+ subscriber .close ()
161
171
# [END pubsub_update_push_configuration]
162
172
163
173
@@ -188,12 +198,14 @@ def callback(message):
188
198
)
189
199
print ("Listening for messages on {}..\n " .format (subscription_path ))
190
200
191
- # result() in a future will block indefinitely if `timeout` is not set,
192
- # unless an exception is encountered first.
193
- try :
194
- streaming_pull_future .result (timeout = timeout )
195
- except : # noqa
196
- streaming_pull_future .cancel ()
201
+ # Wrap subscriber in a 'with' block to automatically call close() when done.
202
+ with subscriber :
203
+ try :
204
+ # When `timeout` is not set, result() will block indefinitely,
205
+ # unless an exception is encountered first.
206
+ streaming_pull_future .result (timeout = timeout )
207
+ except : # noqa
208
+ streaming_pull_future .cancel ()
197
209
# [END pubsub_subscriber_async_pull]
198
210
# [END pubsub_quickstart_subscriber]
199
211
@@ -230,12 +242,14 @@ def callback(message):
230
242
)
231
243
print ("Listening for messages on {}..\n " .format (subscription_path ))
232
244
233
- # result() in a future will block indefinitely if `timeout` is not set,
234
- # unless an exception is encountered first.
235
- try :
236
- streaming_pull_future .result (timeout = timeout )
237
- except : # noqa
238
- streaming_pull_future .cancel ()
245
+ # Wrap subscriber in a 'with' block to automatically call close() when done.
246
+ with subscriber :
247
+ try :
248
+ # When `timeout` is not set, result() will block indefinitely,
249
+ # unless an exception is encountered first.
250
+ streaming_pull_future .result (timeout = timeout )
251
+ except : # noqa
252
+ streaming_pull_future .cancel ()
239
253
# [END pubsub_subscriber_async_pull_custom_attributes]
240
254
# [END pubsub_subscriber_sync_pull_custom_attributes]
241
255
@@ -269,12 +283,14 @@ def callback(message):
269
283
)
270
284
print ("Listening for messages on {}..\n " .format (subscription_path ))
271
285
272
- # result() in a future will block indefinitely if `timeout` is not set,
273
- # unless an exception is encountered first.
274
- try :
275
- streaming_pull_future .result (timeout = timeout )
276
- except : # noqa
277
- streaming_pull_future .cancel ()
286
+ # Wrap subscriber in a 'with' block to automatically call close() when done.
287
+ with subscriber :
288
+ try :
289
+ # When `timeout` is not set, result() will block indefinitely,
290
+ # unless an exception is encountered first.
291
+ streaming_pull_future .result (timeout = timeout )
292
+ except : # noqa
293
+ streaming_pull_future .cancel ()
278
294
# [END pubsub_subscriber_flow_settings]
279
295
280
296
@@ -309,6 +325,8 @@ def synchronous_pull(project_id, subscription_name):
309
325
len (response .received_messages )
310
326
)
311
327
)
328
+
329
+ subscriber .close ()
312
330
# [END pubsub_subscriber_sync_pull]
313
331
314
332
@@ -398,6 +416,8 @@ def worker(msg):
398
416
len (response .received_messages )
399
417
)
400
418
)
419
+
420
+ subscriber .close ()
401
421
# [END pubsub_subscriber_sync_pull_with_lease]
402
422
403
423
@@ -425,17 +445,19 @@ def callback(message):
425
445
)
426
446
print ("Listening for messages on {}..\n " .format (subscription_path ))
427
447
428
- # result() in a future will block indefinitely if `timeout` is not set,
429
- # unless an exception is encountered first.
430
- try :
431
- streaming_pull_future .result (timeout = timeout )
432
- except Exception as e :
433
- streaming_pull_future .cancel ()
434
- print (
435
- "Listening for messages on {} threw an exception: {}." .format (
436
- subscription_name , e
448
+ # Wrap subscriber in a 'with' block to automatically call close() when done.
449
+ with subscriber :
450
+ # When `timeout` is not set, result() will block indefinitely,
451
+ # unless an exception is encountered first.
452
+ try :
453
+ streaming_pull_future .result (timeout = timeout )
454
+ except Exception as e :
455
+ streaming_pull_future .cancel ()
456
+ print (
457
+ "Listening for messages on {} threw an exception: {}." .format (
458
+ subscription_name , e
459
+ )
437
460
)
438
- )
439
461
# [END pubsub_subscriber_error_listener]
440
462
441
463
0 commit comments