Skip to content

Commit

Permalink
Pub/Sub: wrap subscriber in a with block and add comments [(#4070)](G…
Browse files Browse the repository at this point in the history
…oogleCloudPlatform/python-docs-samples#4070)

Use a `with` block to wrap subscriber and describe its purpose. 

Internal bug: b/157401623
  • Loading branch information
anguillanneuf authored and plamut committed Jul 10, 2020
1 parent a40dcf0 commit 0686bfb
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions samples/snippets/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ def list_subscriptions_in_project(project_id):
subscriber = pubsub_v1.SubscriberClient()
project_path = subscriber.project_path(project_id)

for subscription in subscriber.list_subscriptions(project_path):
print(subscription.name)

subscriber.close()
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
for subscription in subscriber.list_subscriptions(project_path):
print(subscription.name)
# [END pubsub_list_subscriptions]


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

subscription = subscriber.create_subscription(subscription_path, topic_path)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(subscription_path, topic_path)

print("Subscription created: {}".format(subscription))

subscriber.close()
# [END pubsub_create_pull_subscription]


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

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

subscription = subscriber.create_subscription(
subscription_path, topic_path, push_config
)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
subscription_path, topic_path, push_config
)

print("Push subscription created: {}".format(subscription))
print("Endpoint for subscription is: {}".format(endpoint))

subscriber.close()
# [END pubsub_create_push_subscription]


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

subscriber.delete_subscription(subscription_path)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscriber.delete_subscription(subscription_path)

print("Subscription deleted: {}".format(subscription_path))

subscriber.close()
# [END pubsub_delete_subscription]


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

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

result = subscriber.update_subscription(subscription, update_mask)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
result = subscriber.update_subscription(subscription, update_mask)

print("Subscription updated: {}".format(subscription_path))
print("New endpoint for subscription is: {}".format(result.push_config))

subscriber.close()
# [END pubsub_update_push_configuration]


Expand Down Expand Up @@ -436,24 +441,25 @@ def synchronous_pull(project_id, subscription_id):

NUM_MESSAGES = 3

# The subscriber pulls a specific number of messages.
response = subscriber.pull(subscription_path, max_messages=NUM_MESSAGES)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
# The subscriber pulls a specific number of messages.
response = subscriber.pull(subscription_path, max_messages=NUM_MESSAGES)

ack_ids = []
for received_message in response.received_messages:
print("Received: {}".format(received_message.message.data))
ack_ids.append(received_message.ack_id)
ack_ids = []
for received_message in response.received_messages:
print("Received: {}".format(received_message.message.data))
ack_ids.append(received_message.ack_id)

# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)
# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)

print(
"Received and acknowledged {} messages. Done.".format(
len(response.received_messages)
print(
"Received and acknowledged {} messages. Done.".format(
len(response.received_messages)
)
)
)

subscriber.close()
# [END pubsub_subscriber_sync_pull]


Expand Down Expand Up @@ -539,6 +545,8 @@ def worker(msg):
)
)

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

Expand Down

0 comments on commit 0686bfb

Please sign in to comment.