@@ -161,16 +161,16 @@ def update_subscription(project_id, subscription_name, endpoint):
161
161
# [END pubsub_update_push_configuration]
162
162
163
163
164
- def receive_messages (project_id , subscription_name ):
164
+ def receive_messages (project_id , subscription_name , timeout = None ):
165
165
"""Receives messages from a pull subscription."""
166
166
# [START pubsub_subscriber_async_pull]
167
167
# [START pubsub_quickstart_subscriber]
168
- import time
169
-
170
168
from google .cloud import pubsub_v1
171
169
172
170
# TODO project_id = "Your Google Cloud Project ID"
173
171
# TODO subscription_name = "Your Pub/Sub subscription name"
172
+ # TODO timeout = 5.0 # "How long the subscriber should listen for
173
+ # messages in seconds"
174
174
175
175
subscriber = pubsub_v1 .SubscriberClient ()
176
176
# The `subscription_path` method creates a fully qualified identifier
@@ -183,27 +183,33 @@ def callback(message):
183
183
print ("Received message: {}" .format (message ))
184
184
message .ack ()
185
185
186
- subscriber .subscribe (subscription_path , callback = callback )
186
+ streaming_pull_future = subscriber .subscribe (
187
+ subscription_path , callback = callback
188
+ )
189
+ print ("Listening for messages on {}..\n " .format (subscription_path ))
187
190
188
- # The subscriber is non-blocking. We must keep the main thread from
189
- # exiting to allow it to process messages asynchronously in the background.
190
- print ("Listening for messages on {}" .format (subscription_path ))
191
- while True :
192
- time .sleep (60 )
191
+ # result() in a future will block indefinitely if `timeout` is not set,
192
+ # unless an exception is encountered first.
193
+ try :
194
+ streaming_pull_future .result (timeout = timeout )
195
+ except : # noqa
196
+ streaming_pull_future .cancel ()
193
197
# [END pubsub_subscriber_async_pull]
194
198
# [END pubsub_quickstart_subscriber]
195
199
196
200
197
- def receive_messages_with_custom_attributes (project_id , subscription_name ):
201
+ def receive_messages_with_custom_attributes (
202
+ project_id , subscription_name , timeout = None
203
+ ):
198
204
"""Receives messages from a pull subscription."""
199
205
# [START pubsub_subscriber_sync_pull_custom_attributes]
200
206
# [START pubsub_subscriber_async_pull_custom_attributes]
201
- import time
202
-
203
207
from google .cloud import pubsub_v1
204
208
205
209
# TODO project_id = "Your Google Cloud Project ID"
206
210
# TODO subscription_name = "Your Pub/Sub subscription name"
211
+ # TODO timeout = 5.0 # "How long the subscriber should listen for
212
+ # messages in seconds"
207
213
208
214
subscriber = pubsub_v1 .SubscriberClient ()
209
215
subscription_path = subscriber .subscription_path (
@@ -219,26 +225,32 @@ def callback(message):
219
225
print ("{}: {}" .format (key , value ))
220
226
message .ack ()
221
227
222
- subscriber .subscribe (subscription_path , callback = callback )
228
+ streaming_pull_future = subscriber .subscribe (
229
+ subscription_path , callback = callback
230
+ )
231
+ print ("Listening for messages on {}..\n " .format (subscription_path ))
223
232
224
- # The subscriber is non-blocking, so we must keep the main thread from
225
- # exiting to allow it to process messages in the background.
226
- print ("Listening for messages on {}" .format (subscription_path ))
227
- while True :
228
- time .sleep (60 )
233
+ # result() in a future will block indefinitely if `timeout` is not set,
234
+ # unless an exception is encountered first.
235
+ try :
236
+ streaming_pull_future .result (timeout = timeout )
237
+ except : # noqa
238
+ streaming_pull_future .cancel ()
229
239
# [END pubsub_subscriber_async_pull_custom_attributes]
230
240
# [END pubsub_subscriber_sync_pull_custom_attributes]
231
241
232
242
233
- def receive_messages_with_flow_control (project_id , subscription_name ):
243
+ def receive_messages_with_flow_control (
244
+ project_id , subscription_name , timeout = None
245
+ ):
234
246
"""Receives messages from a pull subscription with flow control."""
235
247
# [START pubsub_subscriber_flow_settings]
236
- import time
237
-
238
248
from google .cloud import pubsub_v1
239
249
240
250
# TODO project_id = "Your Google Cloud Project ID"
241
251
# TODO subscription_name = "Your Pub/Sub subscription name"
252
+ # TODO timeout = 5.0 # "How long the subscriber should listen for
253
+ # messages in seconds"
242
254
243
255
subscriber = pubsub_v1 .SubscriberClient ()
244
256
subscription_path = subscriber .subscription_path (
@@ -251,15 +263,18 @@ def callback(message):
251
263
252
264
# Limit the subscriber to only have ten outstanding messages at a time.
253
265
flow_control = pubsub_v1 .types .FlowControl (max_messages = 10 )
254
- subscriber .subscribe (
266
+
267
+ streaming_pull_future = subscriber .subscribe (
255
268
subscription_path , callback = callback , flow_control = flow_control
256
269
)
270
+ print ("Listening for messages on {}..\n " .format (subscription_path ))
257
271
258
- # The subscriber is non-blocking, so we must keep the main thread from
259
- # exiting to allow it to process messages in the background.
260
- print ("Listening for messages on {}" .format (subscription_path ))
261
- while True :
262
- time .sleep (60 )
272
+ # result() in a future will block indefinitely if `timeout` is not set,
273
+ # unless an exception is encountered first.
274
+ try :
275
+ streaming_pull_future .result (timeout = timeout )
276
+ except : # noqa
277
+ streaming_pull_future .cancel ()
263
278
# [END pubsub_subscriber_flow_settings]
264
279
265
280
@@ -386,13 +401,15 @@ def worker(msg):
386
401
# [END pubsub_subscriber_sync_pull_with_lease]
387
402
388
403
389
- def listen_for_errors (project_id , subscription_name ):
404
+ def listen_for_errors (project_id , subscription_name , timeout = None ):
390
405
"""Receives messages and catches errors from a pull subscription."""
391
406
# [START pubsub_subscriber_error_listener]
392
407
from google .cloud import pubsub_v1
393
408
394
409
# TODO project_id = "Your Google Cloud Project ID"
395
410
# TODO subscription_name = "Your Pubsub subscription name"
411
+ # TODO timeout = 5.0 # "How long the subscriber should listen for
412
+ # messages in seconds"
396
413
397
414
subscriber = pubsub_v1 .SubscriberClient ()
398
415
subscription_path = subscriber .subscription_path (
@@ -403,16 +420,19 @@ def callback(message):
403
420
print ("Received message: {}" .format (message ))
404
421
message .ack ()
405
422
406
- future = subscriber .subscribe (subscription_path , callback = callback )
423
+ streaming_pull_future = subscriber .subscribe (
424
+ subscription_path , callback = callback
425
+ )
426
+ print ("Listening for messages on {}..\n " .format (subscription_path ))
407
427
408
- # Blocks the thread while messages are coming in through the stream. Any
409
- # exceptions that crop up on the thread will be set on the future .
428
+ # result() in a future will block indefinitely if `timeout` is not set,
429
+ # unless an exception is encountered first .
410
430
try :
411
- # When timeout is unspecified, the result method waits indefinitely.
412
- future .result (timeout = 30 )
431
+ streaming_pull_future .result (timeout = timeout )
413
432
except Exception as e :
433
+ streaming_pull_future .cancel ()
414
434
print (
415
- "Listening for messages on {} threw an Exception : {}." .format (
435
+ "Listening for messages on {} threw an exception : {}." .format (
416
436
subscription_name , e
417
437
)
418
438
)
@@ -518,14 +538,14 @@ def callback(message):
518
538
args .project_id , args .subscription_name , args .endpoint
519
539
)
520
540
elif args .command == "receive" :
521
- receive_messages (args .project_id , args .subscription_name )
541
+ receive_messages (args .project_id , args .subscription_name , args . timeout )
522
542
elif args .command == "receive-custom-attributes" :
523
543
receive_messages_with_custom_attributes (
524
- args .project_id , args .subscription_name
544
+ args .project_id , args .subscription_name , args . timeout
525
545
)
526
546
elif args .command == "receive-flow-control" :
527
547
receive_messages_with_flow_control (
528
- args .project_id , args .subscription_name
548
+ args .project_id , args .subscription_name , args . timeout
529
549
)
530
550
elif args .command == "receive-synchronously" :
531
551
synchronous_pull (args .project_id , args .subscription_name )
@@ -534,4 +554,6 @@ def callback(message):
534
554
args .project_id , args .subscription_name
535
555
)
536
556
elif args .command == "listen_for_errors" :
537
- listen_for_errors (args .project_id , args .subscription_name )
557
+ listen_for_errors (
558
+ args .project_id , args .subscription_name , args .timeout
559
+ )
0 commit comments