-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the Pub/Sub handle_publisher_error sample #1440
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
""" | ||
|
||
import argparse | ||
import concurrent.futures | ||
|
||
from google.cloud import pubsub_v1 | ||
|
||
|
@@ -109,6 +110,36 @@ def publish_messages_with_futures(project, topic_name): | |
print(future.result()) | ||
|
||
|
||
def publish_messages_with_error_handler(project, topic_name): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add region tags. See the work Alix is doing in #1491 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Tim! Most Python doc uses indented_block instead of region_tag to embed code snippets. Should I still add region tags? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please. It's required for go/samples-tracker to work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Region tag added. Sorry for the late response :( |
||
"""Publishes multiple messages to a Pub/Sub topic with an error handler.""" | ||
publisher = pubsub_v1.PublisherClient() | ||
topic_path = publisher.topic_path(project, topic_name) | ||
|
||
# When you publish a message, the client returns a Future. This Future | ||
# can be used to track if an error has occurred. | ||
futures = [] | ||
|
||
def callback(f): | ||
e = f.exception() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if e: | ||
print('Publishing message on {} threw an Exception {}.'.format( | ||
topic_name, e)) | ||
|
||
for n in range(1, 10): | ||
data = u'Message number {}'.format(n) | ||
# Data must be a bytestring | ||
data = data.encode('utf-8') | ||
message_future = publisher.publish(topic_path, data=data) | ||
message_future.add_done_callback(callback) | ||
futures.append(message_future) | ||
|
||
# We must keep the main thread from exiting to allow it to process | ||
# messages in the background. | ||
concurrent.futures.wait(futures) | ||
|
||
print('Published messages.') | ||
|
||
|
||
def publish_messages_with_batch_settings(project, topic_name): | ||
"""Publishes multiple messages to a Pub/Sub topic with batch settings.""" | ||
# Configure the batch to publish once there is one kilobyte of data or | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should you explicitly add the futures backport to requirements.txt for Python 2.7 compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done; requirements.txt updated.