@@ -95,7 +95,7 @@ def publish_messages(project_id, topic_name):
95
95
data = data .encode ('utf-8' )
96
96
# When you publish a message, the client returns a future.
97
97
future = publisher .publish (topic_path , data = data )
98
- print ('Published {} of message ID {}.' . format ( data , future .result () ))
98
+ print (future .result ())
99
99
100
100
print ('Published messages.' )
101
101
# [END pubsub_quickstart_publisher]
@@ -119,8 +119,9 @@ def publish_messages_with_custom_attributes(project_id, topic_name):
119
119
# Data must be a bytestring
120
120
data = data .encode ('utf-8' )
121
121
# Add two attributes, origin and username, to the message
122
- publisher .publish (
122
+ future = publisher .publish (
123
123
topic_path , data , origin = 'python-sample' , username = 'gcp' )
124
+ print (future .result ())
124
125
125
126
print ('Published messages with custom attributes.' )
126
127
# [END pubsub_publish_custom_attributes]
@@ -138,21 +139,15 @@ def publish_messages_with_futures(project_id, topic_name):
138
139
publisher = pubsub_v1 .PublisherClient ()
139
140
topic_path = publisher .topic_path (project_id , topic_name )
140
141
141
- # When you publish a message, the client returns a Future. This Future
142
- # can be used to track when the message is published.
143
- futures = []
144
-
145
142
for n in range (1 , 10 ):
146
143
data = u'Message number {}' .format (n )
147
144
# Data must be a bytestring
148
145
data = data .encode ('utf-8' )
149
- message_future = publisher .publish (topic_path , data = data )
150
- futures .append (message_future )
151
-
152
- print ('Published message IDs:' )
153
- for future in futures :
154
- # result() blocks until the message is published.
146
+ # When you publish a message, the client returns a future.
147
+ future = publisher .publish (topic_path , data = data )
155
148
print (future .result ())
149
+
150
+ print ("Published messages with futures." )
156
151
# [END pubsub_publisher_concurrency_control]
157
152
158
153
@@ -169,28 +164,34 @@ def publish_messages_with_error_handler(project_id, topic_name):
169
164
publisher = pubsub_v1 .PublisherClient ()
170
165
topic_path = publisher .topic_path (project_id , topic_name )
171
166
172
- def callback (message_future ):
173
- if message_future .exception ():
174
- print ('{} needs handling.' .format (message_future .exception ()))
175
- else :
176
- print (message_future .result ())
167
+ futures = dict ()
177
168
178
- for n in range (1 , 10 ):
179
- data = u'Message number {}' .format (n )
180
- # Data must be a bytestring
181
- data = data .encode ('utf-8' )
182
- # When you publish a message, the client returns a Future.
183
- message_future = publisher .publish (topic_path , data = data )
184
- # If you wish to handle publish failures, do it in the callback.
185
- # Otherwise, it's okay to call `message_future.result()` directly.
186
- message_future .add_done_callback (callback )
187
-
188
- print ('Published message IDs:' )
189
-
190
- # We keep the main thread from exiting so message futures can be
191
- # resolved in the background.
192
- while True :
193
- time .sleep (60 )
169
+ def get_callback (f , data ):
170
+ def callback (f ):
171
+ try :
172
+ print (f .result ())
173
+ futures .pop (data )
174
+ except : # noqa
175
+ print ("Please handle {} for {}." .format (f .exception (), data ))
176
+ return callback
177
+
178
+ for i in range (10 ):
179
+ data = str (i )
180
+ futures .update ({data : None })
181
+ # When you publish a message, the client returns a future.
182
+ future = publisher .publish (
183
+ topic_path ,
184
+ data = data .encode ("utf-8" ), # data must be a bytestring.
185
+ )
186
+ futures [data ] = future
187
+ # Publish failures shall be handled in the callback function.
188
+ future .add_done_callback (get_callback (future , data ))
189
+
190
+ # Wait for all the publish futures to resolve before exiting.
191
+ while futures :
192
+ time .sleep (5 )
193
+
194
+ print ("Published message with error handler." )
194
195
# [END pubsub_publish_messages_error_handler]
195
196
196
197
@@ -215,9 +216,10 @@ def publish_messages_with_batch_settings(project_id, topic_name):
215
216
data = u'Message number {}' .format (n )
216
217
# Data must be a bytestring
217
218
data = data .encode ('utf-8' )
218
- publisher .publish (topic_path , data = data )
219
+ future = publisher .publish (topic_path , data = data )
220
+ print (future .result ())
219
221
220
- print ('Published messages.' )
222
+ print ('Published messages with batch settings .' )
221
223
# [END pubsub_publisher_batch_settings]
222
224
223
225
0 commit comments