Skip to content
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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pubsub/cloud-client/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"""

import argparse
import concurrent.futures
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done; requirements.txt updated.


from google.cloud import pubsub_v1

Expand Down Expand Up @@ -109,6 +110,36 @@ def publish_messages_with_futures(project, topic_name):
print(future.result())


def publish_messages_with_error_handler(project, topic_name):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add region tags. See the work Alix is doing in #1491

Copy link
Contributor Author

@michaelawyu michaelawyu May 18, 2018

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please. It's required for go/samples-tracker to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

@tswast tswast May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e is too short. Use exc

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
Expand Down