Skip to content

Commit 28d35a1

Browse files
anguillanneufplamut
authored andcommitted
Added timeout in error handling [(#1636)](GoogleCloudPlatform/python-docs-samples#1636)
1 parent 3b41ec3 commit 28d35a1

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

samples/snippets/publisher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ def publish_messages_with_error_handler(project, topic_name):
131131
topic_path = publisher.topic_path(project, topic_name)
132132

133133
def callback(message_future):
134-
if message_future.exception():
134+
# When timeout is unspecified, the exception method waits indefinitely.
135+
if message_future.exception(timeout=30):
135136
print('Publishing message on {} threw an Exception {}.'.format(
136137
topic_name, message_future.exception()))
137138
else:

samples/snippets/subscriber.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,12 +223,12 @@ def callback(message):
223223
# Blocks the thread while messages are coming in through the stream. Any
224224
# exceptions that crop up on the thread will be set on the future.
225225
try:
226-
subscription.future.result()
226+
# When timeout is unspecified, the result method waits indefinitely.
227+
subscription.future.result(timeout=30)
227228
except Exception as e:
228229
print(
229230
'Listening for messages on {} threw an Exception: {}.'.format(
230231
subscription_name, e))
231-
raise
232232
# [END pubsub_subscriber_error_listener]
233233

234234

0 commit comments

Comments
 (0)