Skip to content

Commit 470db86

Browse files
Pub/Sub: wrap subscriber in a with block and add comments [(#4070)](GoogleCloudPlatform/python-docs-samples#4070)
Use a `with` block to wrap subscriber and describe its purpose. Internal bug: b/157401623
1 parent 2c87a45 commit 470db86

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

samples/snippets/subscriber.py

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ def list_subscriptions_in_project(project_id):
5252
subscriber = pubsub_v1.SubscriberClient()
5353
project_path = subscriber.project_path(project_id)
5454

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)
5960
# [END pubsub_list_subscriptions]
6061

6162

@@ -73,11 +74,12 @@ def create_subscription(project_id, topic_id, subscription_id):
7374
topic_path = subscriber.topic_path(project_id, topic_id)
7475
subscription_path = subscriber.subscription_path(project_id, subscription_id)
7576

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)
7781

7882
print("Subscription created: {}".format(subscription))
79-
80-
subscriber.close()
8183
# [END pubsub_create_pull_subscription]
8284

8385

@@ -146,14 +148,15 @@ def create_push_subscription(project_id, topic_id, subscription_id, endpoint):
146148

147149
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)
148150

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+
)
152157

153158
print("Push subscription created: {}".format(subscription))
154159
print("Endpoint for subscription is: {}".format(endpoint))
155-
156-
subscriber.close()
157160
# [END pubsub_create_push_subscription]
158161

159162

@@ -169,11 +172,12 @@ def delete_subscription(project_id, subscription_id):
169172
subscriber = pubsub_v1.SubscriberClient()
170173
subscription_path = subscriber.subscription_path(project_id, subscription_id)
171174

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)
173179

174180
print("Subscription deleted: {}".format(subscription_path))
175-
176-
subscriber.close()
177181
# [END pubsub_delete_subscription]
178182

179183

@@ -203,12 +207,13 @@ def update_push_subscription(project_id, topic_id, subscription_id, endpoint):
203207

204208
update_mask = {"paths": {"push_config"}}
205209

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)
207214

208215
print("Subscription updated: {}".format(subscription_path))
209216
print("New endpoint for subscription is: {}".format(result.push_config))
210-
211-
subscriber.close()
212217
# [END pubsub_update_push_configuration]
213218

214219

@@ -436,24 +441,25 @@ def synchronous_pull(project_id, subscription_id):
436441

437442
NUM_MESSAGES = 3
438443

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)
441449

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)
446454

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)
449457

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+
)
453462
)
454-
)
455-
456-
subscriber.close()
457463
# [END pubsub_subscriber_sync_pull]
458464

459465

@@ -539,6 +545,8 @@ def worker(msg):
539545
)
540546
)
541547

548+
# Close the underlying gPRC channel. Alternatively, wrap subscriber in
549+
# a 'with' block to automatically call close() when done.
542550
subscriber.close()
543551
# [END pubsub_subscriber_sync_pull_with_lease]
544552

0 commit comments

Comments
 (0)