Skip to main content

Python Client for Google Cloud Pub/Sub

Project description

Python idiomatic client for Google Cloud Pub / Sub

pypi versions

Quick Start

$ pip install --upgrade google-cloud-pubsub

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 = '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()
publisher.publish(topic, 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 = '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.
)
subscription = subscriber.create_subscription(topic, subscription)

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

google-cloud-pubsub-0.28.4.tar.gz (71.0 kB view details)

Uploaded Source

Built Distribution

google_cloud_pubsub-0.28.4-py2.py3-none-any.whl (79.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file google-cloud-pubsub-0.28.4.tar.gz.

File metadata

File hashes

Hashes for google-cloud-pubsub-0.28.4.tar.gz
Algorithm Hash digest
SHA256 ca66528d0b2e7244bdab9c5cd57e7d0a1e4ef568959656107f1175e4b4065ed0
MD5 7c70d82a1b58623c1fdf56a34640f542
BLAKE2b-256 015019fb3338d72dd5c0fc7c63ffc2e7b716a71e25a63f4fa79ee7eff56ad164

See more details on using hashes here.

File details

Details for the file google_cloud_pubsub-0.28.4-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for google_cloud_pubsub-0.28.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8125b68117dfc1bd94bff030e6ed5f85528a97f456e942539643eb4088df7eac
MD5 3a4afe92dc72fd657d15791ffd620946
BLAKE2b-256 37afdae3e760b4e87e221840331e4f5d4860cc0d7de365cc97a69aaf3d5558e5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page