for n in range(1, 10):
data = u"Message number {}".format(n)
// Data must be a bytestring
data = data.encode("utf-8")
// When you publish a message, the client returns a Future.
message_future = publisher.publish(topic_path, data=data)
// If you wish to handle publish failures, do it in the callback.
// Otherwise, it"s okay to call `message_future.result()` directly.
After Change
// When you publish a message, the client returns a future.
future = publisher.publish(
topic_path,
data=data.encode("utf-8"), // data must be a bytestring.
)
futures[data] = future
// Publish failures shall be handled in the callback function.
future.add_done_callback(get_callback(future, data))
// Wait for all the publish futures to resolve before exiting.