Skip to content

Commit

Permalink
docs: update samples in documentation (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf authored Feb 17, 2021
1 parent a8c8f53 commit 999a42f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
12 changes: 7 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ messages to it
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
publisher.create_topic(topic_name)
publisher.publish(topic_name, b'My first message!', spam='eggs')
future = publisher.publish(topic_name, b'My first message!', spam='eggs')
future.result()
To learn more, consult the `publishing documentation`_.

Expand All @@ -129,23 +130,24 @@ the topic, and subscribe to that, passing a callback function.
import os
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
)
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
def callback(message):
print(message.data)
message.ack()
future = subscriber.subscribe(subscription_name, callback)
with pubsub_v1.SubscriberClient() as subscriber:
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
future = subscriber.subscribe(subscription_name, callback)
The future returned by the call to ``subscriber.subscribe`` can be used to
block the current thread until a given condition obtains:
Expand Down
4 changes: 2 additions & 2 deletions docs/publisher/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Therefore, a very basic publishing call looks like:
.. code-block:: python
topic = 'projects/{project}/topics/{topic}'
publish_client.publish(topic, b'This is my message.')
future = publish_client.publish(topic, b'This is my message.')
.. note::

Expand All @@ -52,7 +52,7 @@ If you want to include attributes, simply add keyword arguments:
.. code-block:: python
topic = 'projects/{project}/topics/{topic}'
publish_client.publish(topic, b'This is my message.', foo='bar')
future = publish_client.publish(topic, b'This is my message.', foo='bar')
Batching
Expand Down
20 changes: 16 additions & 4 deletions docs/subscriber/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Instantiating a subscriber client is straightforward:
.. code-block:: python
from google.cloud import pubsub
subscriber = pubsub.SubscriberClient()
with pubsub.SubscriberClient() as subscriber:
# ...
Creating a Subscription
-----------------------
Expand Down Expand Up @@ -41,8 +42,10 @@ to subscribe to, and it must already exist. Once you have that, it is easy:
# publisher = pubsub.PublisherClient()
topic_path = publisher.topic_path(PROJECT, TOPIC)
sub_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
subscriber.create_subscription(request={"name": sub_path, "topic": topic_path})
with pubsub.SubscriberClient() as subscriber:
sub_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
subscriber.create_subscription(request={"name": sub_path, "topic": topic_path})
Once you have created a subscription (or if you already had one), the next
step is to pull data from it.
Expand All @@ -56,6 +59,8 @@ To pull the messages synchronously, use the client's

.. code-block:: python
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
# application.
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
Expand Down Expand Up @@ -88,6 +93,8 @@ be dropped by this client and the backend will try to re-deliver them.

.. code-block:: python
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
ack_ids = [] # TODO: populate with `ack_ids` of the messages to NACK
ack_deadline_seconds = 0
subscriber.modify_ack_deadline(
Expand All @@ -109,6 +116,8 @@ each message received.

.. code-block:: python
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
# application.
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
Expand Down Expand Up @@ -147,6 +156,8 @@ Here is an example:
do_something_with(message) # Replace this with your actual logic.
message.ack() # Asynchronously acknowledge the message.
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
# application.
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
Expand Down Expand Up @@ -177,7 +188,8 @@ thread will be set on the future.
try:
future.result()
except Exception as ex:
subscription.close()
# Close the subscriber if not using a context manager.
subscriber.close()
raise
Finally, you can use
Expand Down

0 comments on commit 999a42f

Please sign in to comment.