Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update subscriber example in README to current patterns. #6194

Merged
merged 3 commits into from
Oct 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions pubsub/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ messages to it
.. code-block:: python
import os
from google.cloud import pubsub
from google.cloud import pubsub_v1
publisher = pubsub.PublisherClient()
publisher = pubsub_v1.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
Expand All @@ -109,14 +109,14 @@ Subscribing
^^^^^^^^^^^

To subscribe to data in Cloud Pub/Sub, you create a subscription based on
the topic, and subscribe to that.
the topic, and subscribe to that, passing a callback function.

.. code-block:: python
import os
from google.cloud import pubsub
from google.cloud import pubsub_v1
subscriber = pubsub.SubscriberClient()
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.
Expand All @@ -127,17 +127,22 @@ the topic, and subscribe to that.
)
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
subscription = subscriber.subscribe(subscription_name)
The subscription is opened asychronously, and messages are processed by
use of a callback.

.. code-block:: python
def callback(message):
print(message.data)
message.ack()
subscription.open(callback)
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:

.. code-block:: python
try:
future.result()
except KeyboardInterrupt:
future.cancel()
To learn more, consult the `subscriber documentation`_.

Expand Down