Skip to content

Commit 86cb20a

Browse files
docs: update samples in documentation (#254)
1 parent 043b08d commit 86cb20a

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

packages/google-cloud-pubsub/README.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ messages to it
111111
topic='MY_TOPIC_NAME', # Set this to something appropriate.
112112
)
113113
publisher.create_topic(topic_name)
114-
publisher.publish(topic_name, b'My first message!', spam='eggs')
114+
future = publisher.publish(topic_name, b'My first message!', spam='eggs')
115+
future.result()
115116
116117
To learn more, consult the `publishing documentation`_.
117118

@@ -129,23 +130,24 @@ the topic, and subscribe to that, passing a callback function.
129130
import os
130131
from google.cloud import pubsub_v1
131132
132-
subscriber = pubsub_v1.SubscriberClient()
133133
topic_name = 'projects/{project_id}/topics/{topic}'.format(
134134
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
135135
topic='MY_TOPIC_NAME', # Set this to something appropriate.
136136
)
137+
137138
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
138139
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
139140
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
140141
)
141-
subscriber.create_subscription(
142-
name=subscription_name, topic=topic_name)
143142
144143
def callback(message):
145144
print(message.data)
146145
message.ack()
147146
148-
future = subscriber.subscribe(subscription_name, callback)
147+
with pubsub_v1.SubscriberClient() as subscriber:
148+
subscriber.create_subscription(
149+
name=subscription_name, topic=topic_name)
150+
future = subscriber.subscribe(subscription_name, callback)
149151
150152
The future returned by the call to ``subscriber.subscribe`` can be used to
151153
block the current thread until a given condition obtains:

packages/google-cloud-pubsub/docs/publisher/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Therefore, a very basic publishing call looks like:
3333
.. code-block:: python
3434
3535
topic = 'projects/{project}/topics/{topic}'
36-
publish_client.publish(topic, b'This is my message.')
36+
future = publish_client.publish(topic, b'This is my message.')
3737
3838
.. note::
3939

@@ -52,7 +52,7 @@ If you want to include attributes, simply add keyword arguments:
5252
.. code-block:: python
5353
5454
topic = 'projects/{project}/topics/{topic}'
55-
publish_client.publish(topic, b'This is my message.', foo='bar')
55+
future = publish_client.publish(topic, b'This is my message.', foo='bar')
5656
5757
5858
Batching

packages/google-cloud-pubsub/docs/subscriber/index.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ Instantiating a subscriber client is straightforward:
1212
.. code-block:: python
1313
1414
from google.cloud import pubsub
15-
subscriber = pubsub.SubscriberClient()
1615
16+
with pubsub.SubscriberClient() as subscriber:
17+
# ...
1718
1819
Creating a Subscription
1920
-----------------------
@@ -41,8 +42,10 @@ to subscribe to, and it must already exist. Once you have that, it is easy:
4142
# publisher = pubsub.PublisherClient()
4243
4344
topic_path = publisher.topic_path(PROJECT, TOPIC)
44-
sub_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
45-
subscriber.create_subscription(request={"name": sub_path, "topic": topic_path})
45+
46+
with pubsub.SubscriberClient() as subscriber:
47+
sub_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
48+
subscriber.create_subscription(request={"name": sub_path, "topic": topic_path})
4649
4750
Once you have created a subscription (or if you already had one), the next
4851
step is to pull data from it.
@@ -56,6 +59,8 @@ To pull the messages synchronously, use the client's
5659

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

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

110117
.. code-block:: python
111118
119+
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
120+
112121
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
113122
# application.
114123
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
@@ -147,6 +156,8 @@ Here is an example:
147156
do_something_with(message) # Replace this with your actual logic.
148157
message.ack() # Asynchronously acknowledge the message.
149158
159+
# Wrap the following code in `with pubsub.SubscriberClient() as subscriber:`
160+
150161
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
151162
# application.
152163
subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
@@ -177,7 +188,8 @@ thread will be set on the future.
177188
try:
178189
future.result()
179190
except Exception as ex:
180-
subscription.close()
191+
# Close the subscriber if not using a context manager.
192+
subscriber.close()
181193
raise
182194
183195
Finally, you can use

0 commit comments

Comments
 (0)