Google Cloud Pub/Sub API client library
Project description
Python idiomatic client for Google Cloud Pub / Sub
Quick Start
$ pip install --upgrade google-cloud-pubsub
For more information on setting up your Python development environment, such as installing pip and virtualenv on your system, please refer to Python Development Environment Setup Guide for Google Cloud Platform.
Authentication
With google-cloud-python we try to make authentication as painless as possible. Check out the Authentication section in our documentation to learn more. You may also find the authentication document shared by all the google-cloud-* libraries to be helpful.
Using the API
Google Cloud Pub/Sub (Pub/Sub API docs) is designed to provide reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a topic and other applications can subscribe to that topic to receive the messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows developers to communicate between independently written applications.
See the google-cloud-python API Pub/Sub documentation to learn how to connect to Cloud Pub/Sub using this Client Library.
Publishing
To publish data to Cloud Pub/Sub you must create a topic, and then publish messages to it
import os
from google.cloud import pubsub
publisher = pubsub.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
publisher.create_topic(topic_name)
publisher.publish(topic_name, b'My first message!', spam='eggs')
To learn more, consult the publishing documentation.
Subscribing
To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that.
import os
from google.cloud import pubsub
subscriber = pubsub.SubscriberClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
topic='MY_TOPIC_NAME', # Set this to something appropriate.
)
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
project_id=os.getenv('GOOGLE_CLOUD_PROJECT'),
sub='MY_SUBSCRIPTION_NAME', # Set this to something appropriate.
)
subscriber.create_subscription(
name=subscription_name, topic=topic_name)
subscription = subscriber.subscribe(subscription_name)
The subscription is opened asychronously, and messages are processed by use of a callback.
def callback(message):
print(message.data)
message.ack()
subscription.open(callback)
To learn more, consult the subscriber documentation.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for google-cloud-pubsub-0.32.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6c9fd4c8c2be8eea17c8541a1e114c049f17c0f5f31afc81fe884fc8a6af740 |
|
MD5 | 0d85c37e057c06bc86243e931ace6023 |
|
BLAKE2b-256 | 42f5069178d34f9d58aad0c362948a618cf631656395c7263c0c03377bc60a8f |
Hashes for google_cloud_pubsub-0.32.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67017bab146aeb28137a767452b53d7b31ee128ca8971a6f68347a6d5ac96b00 |
|
MD5 | c9bd1348d73e4c566233a897c5303bd9 |
|
BLAKE2b-256 | 46f55310283fbf716f256d7af698fb76f4a3068a420f805663bcda095fe036bf |