From f4fa8b6dc928f5a4057bfffbcdbc7cdf636006c0 Mon Sep 17 00:00:00 2001 From: Tianzi Cai Date: Wed, 15 Aug 2018 15:59:01 -0700 Subject: [PATCH] Added timeout in error handling [(#1636)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1636) --- samples/snippets/publisher.py | 3 ++- samples/snippets/subscriber.py | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/snippets/publisher.py b/samples/snippets/publisher.py index 96caba9f4..a577abc63 100644 --- a/samples/snippets/publisher.py +++ b/samples/snippets/publisher.py @@ -131,7 +131,8 @@ def publish_messages_with_error_handler(project, topic_name): topic_path = publisher.topic_path(project, topic_name) def callback(message_future): - if message_future.exception(): + # When timeout is unspecified, the exception method waits indefinitely. + if message_future.exception(timeout=30): print('Publishing message on {} threw an Exception {}.'.format( topic_name, message_future.exception())) else: diff --git a/samples/snippets/subscriber.py b/samples/snippets/subscriber.py index 46bb51188..51fa96b86 100644 --- a/samples/snippets/subscriber.py +++ b/samples/snippets/subscriber.py @@ -223,12 +223,12 @@ def callback(message): # Blocks the thread while messages are coming in through the stream. Any # exceptions that crop up on the thread will be set on the future. try: - subscription.future.result() + # When timeout is unspecified, the result method waits indefinitely. + subscription.future.result(timeout=30) except Exception as e: print( 'Listening for messages on {} threw an Exception: {}.'.format( subscription_name, e)) - raise # [END pubsub_subscriber_error_listener]