Skip to main content

Google Cloud Pub/Sub API client library

Project description

Python idiomatic client for Google Cloud Pub / Sub

pypi versions

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.


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.


To publish data to Cloud Pub/Sub you must create a topic, and then publish messages to it

import os
from import pubsub

publisher = pubsub.PublisherClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
    topic='MY_TOPIC_NAME',  # Set this to something appropriate.
publisher.publish(topic_name, b'My first message!', spam='eggs')

To learn more, consult the publishing documentation.


To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that.

import os
from import pubsub

subscriber = pubsub.SubscriberClient()
topic_name = 'projects/{project_id}/topics/{topic}'.format(
    topic='MY_TOPIC_NAME',  # Set this to something appropriate.
subscription_name = 'projects/{project_id}/subscriptions/{sub}'.format(
    sub='MY_SUBSCRIPTION_NAME',  # Set this to something appropriate.
    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):

To learn more, consult the subscriber documentation.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for google-cloud-pubsub, version 0.37.1
Filename, size File type Python version Upload date Hashes
Filename, size google_cloud_pubsub-0.37.1-py2.py3-none-any.whl (99.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size google-cloud-pubsub-0.37.1.tar.gz (96.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page