@@ -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 for your 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
5076background thread to receive messages from Pub/Sub and calls a callback with
5177each 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
7397The 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
76103function should either:meth: `~.pubsub_v1.subscriber.message.Message.ack `
77104or: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.
88115def callback (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
99127a:class: `~.pubsub_v1.subscriber.futures.StreamingPullFuture `, which is both