Skip to content

Commit

Permalink
address peter's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf committed Jun 16, 2021
1 parent 94b8dfb commit 7dd3572
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions samples/snippets/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create_topic(project_id, topic_id):

topic = publisher.create_topic(request={"name": topic_path})

print("Created topic: {}".format(topic.name))
print(f"Created topic: {topic.name}")
# [END pubsub_quickstart_create_topic]
# [END pubsub_create_topic]

Expand All @@ -74,7 +74,7 @@ def delete_topic(project_id, topic_id):

publisher.delete_topic(request={"topic": topic_path})

print("Topic deleted: {}".format(topic_path))
print(f"Topic deleted: {topic_path}")
# [END pubsub_delete_topic]


Expand All @@ -94,7 +94,7 @@ def publish_messages(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = "Message number {}".format(n)
data = f"Message number {n}"
# Data must be a bytestring
data = data.encode("utf-8")
# When you publish a message, the client returns a future.
Expand All @@ -120,7 +120,7 @@ def publish_messages_with_custom_attributes(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = "Message number {}".format(n)
data = f"Message number {n}"
# Data must be a bytestring
data = data.encode("utf-8")
# Add two attributes, origin and username, to the message
Expand Down Expand Up @@ -150,18 +150,18 @@ def publish_messages_with_error_handler(project_id, topic_id):
def get_callback(publish_future, data):
def callback(publish_future):
try:
# Wait half 100 ms for the publish call to succeed.
# Wait 100 ms for the publish call to succeed.
print(publish_future.result(timeout=0.1))
except TimeoutError:
print("Publishing {} timed out.".format(data))
except futures.TimeoutError:
print(f"Publishing {data} timed out.")

return callback

for i in range(10):
data = str(i)
# When you publish a message, the client returns a future.
publish_future = publisher.publish(topic_path, data.encode("utf-8"))
# Non-blocking. Publish failures is handled in the callback function.
# Non-blocking. Publish failures are handled in the callback function.
publish_future.add_done_callback(get_callback(publish_future, data))
publish_futures.append(publish_future)

Expand All @@ -182,11 +182,11 @@ def publish_messages_with_batch_settings(project_id, topic_id):
# project_id = "your-project-id"
# topic_id = "your-topic-id"

# Configure the batch to publish as soon as there is ten messages,
# one kilobyte of data, or one second has passed.
# Configure the batch to publish as soon as there are 10 messages
# oe 1 KiB of data, or 1 second has passed.
batch_settings = pubsub_v1.types.BatchSettings(
max_messages=10, # default 100
max_bytes=1024, # default 1 MB
max_bytes=1024, # default 1 MiB
max_latency=1, # default 10 ms
)
publisher = pubsub_v1.PublisherClient(batch_settings)
Expand All @@ -199,7 +199,7 @@ def callback(future):
print(message_id)

for n in range(1, 10):
data = "Message number {}".format(n)
data = f"Message number {n}"
# Data must be a bytestring
data = data.encode("utf-8")
publish_future = publisher.publish(topic_path, data)
Expand Down Expand Up @@ -248,11 +248,11 @@ def callback(publish_future):

# Publish 1000 messages in quick succession to trigger flow control.
for n in range(1, 1000):
data = "Message number {}".format(n)
data = f"Message number {n}"
# Data must be a bytestring
data = data.encode("utf-8")
publish_future = publisher.publish(topic_path, data)
# Non-blocking.
# Non-blocking. Allow the publisher client to batch messages.
publish_future.add_done_callback(callback)
publish_futures.append(publish_future)

Expand Down Expand Up @@ -294,7 +294,7 @@ def publish_messages_with_retry_settings(project_id, topic_id):
topic_path = publisher.topic_path(project_id, topic_id)

for n in range(1, 10):
data = "Message number {}".format(n)
data = f"Message number {n}"
# Data must be a bytestring
data = data.encode("utf-8")
future = publisher.publish(topic=topic_path, data=data, retry=custom_retry)
Expand Down

0 comments on commit 7dd3572

Please sign in to comment.