Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20f83b1

Browse files
plamutsduskis
authored andcommitted
Document different PuSub received message types (#8468)
1 parent9d9051f commit20f83b1

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

‎pubsub/README.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ block the current thread until a given condition obtains:
155155
exceptKeyboardInterrupt:
156156
future.cancel()
157157
158-
To learn more, consult the `subscriber documentation`_.
158+
It is also possible to pull messages in a synchronous (blocking) fashion. To
159+
learn more about subscribing, consult the `subscriber documentation`_.
159160

160161
.. _subscriber documentation:https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/index.html

‎pubsub/docs/subscriber/index.rst‎

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,54 @@ to subscribe to, and it must already exist. Once you have that, it is easy:
3434

3535
..code-block::python
3636
37-
# Substitute{project}, {topic}, and{subscription} with appropriate
38-
#values foryour application.
39-
topic_name='projects/{project}/topics/{topic}'
40-
sub_name='projects/{project}/subscriptions/{subscription}'
41-
subscriber.create_subscription(sub_name, topic_name)
37+
# SubstitutePROJECT, SUBSCRIPTION, andTOPIC with appropriate values for
38+
# your application.
39+
sub_path=subscriber.subscription_path(PROJECT,SUBSCRIPTION)
40+
topic_path=subscriber.topic_path(PROJECT,TOPIC)
41+
subscriber.create_subscription(sub_path, topic_path)
4242
43+
Once you have created a subscription (or if you already had one), the next
44+
step is to pull data from it.
4345

44-
Pulling a Subscription
45-
----------------------
4646

47-
Once you have created a subscription (or if you already had one), the next
48-
step is to pull data from it. The subscriber client uses the
47+
Pulling a Subscription Synchronously
48+
------------------------------------
49+
50+
To pull the messages synchronously, use the client's
51+
:meth:`~.pubsub_v1.subscriber.client.Client.pull` method.
52+
53+
..code-block::python
54+
55+
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
56+
# application.
57+
subscription_path= subscriber.subscription_path(PROJECT,SUBSCRIPTION)
58+
response= subscriber.pull(subscription_path,max_messages=5)
59+
60+
for msgin response.received_messages:
61+
print("Received message:", msg.message.data)
62+
63+
ack_ids= [msg.ack_idfor msgin response.received_messages]
64+
subscriber.acknowledge(subscription_path, ack_ids)
65+
66+
The method returns a:class:`~.pubsub_v1.types.PullResponse` instance that
67+
cointains a list of received:class:`~.pubsub_v1.types.ReceivedMessage`
68+
instances.
69+
70+
71+
Pulling a Subscription Asynchronously
72+
-------------------------------------
73+
74+
The subscriber client uses the
4975
:meth:`~.pubsub_v1.subscriber.client.Client.subscribe` method to start a
5076
background thread to receive messages from Pub/Sub and calls a callback with
5177
each message received.
5278

5379
..code-block::python
5480
55-
# As before, substitute {project} and {subscription} with appropriate
56-
# values for your application.
57-
future= subscriber.subscribe(
58-
'projects/{project}/subscriptions/{subscription}',
59-
callback
60-
)
81+
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
82+
# application.
83+
subscription_path= subscriber.subscription_path(PROJECT,SUBSCRIPTION)
84+
future= subscriber.subscribe(subscription_path, callback)
6185
6286
This will return a
6387
:class:`~.pubsub_v1.subscriber.futures.StreamingPullFuture`. This future allows
@@ -71,8 +95,11 @@ Messages received from a subscription are processed asynchronously through
7195
**callbacks**.
7296

7397
The basic idea: Define a function that takes one argument; this argument
74-
will be a:class:`~.pubsub_v1.subscriber.message.Message` instance. This
75-
function should do whatever processing is necessary. At the end, the
98+
will be a:class:`~.pubsub_v1.subscriber.message.Message` instance, which is
99+
a convenience wrapper around the:class:`~.pubsub_v1.types.PubsubMessage`
100+
instance received from the server (and stored under the ``message`` attribute).
101+
102+
This function should do whatever processing is necessary. At the end, the
76103
function should either:meth:`~.pubsub_v1.subscriber.message.Message.ack`
77104
or:meth:`~.pubsub_v1.subscriber.message.Message.nack` the message.
78105

@@ -87,13 +114,14 @@ Here is an example:
87114
# Note that the callback is defined *before* the subscription is opened.
88115
defcallback(message):
89116
do_something_with(message)# Replace this with your actual logic.
90-
message.ack()
117+
message.ack()# Asynchronously acknowledge the message.
118+
119+
# Substitute PROJECT and SUBSCRIPTION with appropriate values for your
120+
# application.
121+
subscription_path= subscriber.subscription_path(PROJECT,SUBSCRIPTION)
91122
92123
# Open the subscription, passing the callback.
93-
future= subscriber.subscribe(
94-
'projects/{project}/subscriptions/{subscription}',
95-
callback
96-
)
124+
future= subscriber.subscribe(subscription_path, callback)
97125
98126
The:meth:`~.pubsub_v1.subscriber.client.Client.subscribe` method returns
99127
a:class:`~.pubsub_v1.subscriber.futures.StreamingPullFuture`, which is both

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp