As of January 1, 2020 this library no longer supports Python 2 on the latest released version.
Library versions released prior to that date will continue to be available. For more information please
visit Python 2 support on Google Cloud.
Publisher Client¶
Publish operations are handled through the
PublisherClient
class (aliased as
google.cloud.pubsublite.cloudpubsub.PublisherClient
).
You should instantiate a publisher client using a context manager:
from google.cloud.pubsublite.cloudpubsub import PublisherClient
with PublisherClient() as publisher_client:
# Use publisher_client
When not using a context manager, you need to call
__enter__()
.
Publish a message¶
To publish a message, use the
publish()
method. This method accepts
two positional arguments: a TopicPath
object
and a message in byte string.
A call to publish a message looks like:
from google.cloud.pubsublite.cloudpubsub import PublisherClient
from google.cloud.pubsublite.types import (
CloudRegion, CloudZone, TopicPath,
)
project_number = 1122334455
cloud_region = "us-central1"
zone_id = "a"
topic_id = "your-topic-id"
location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = TopicPath(project_number, location, topic_id)
with PublisherClient() as publisher_client:
data = "Hello world!"
api_future = publisher_client.publish(
topic_path, data.encode("utf-8")
)
message_id = api_future.result()