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.

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

google-cloud-pubsub-0.37.1.tar.gz (96.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

google_cloud_pubsub-0.37.1-py2.py3-none-any.whl (99.7 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: google-cloud-pubsub-0.37.1.tar.gz
  • Upload date:
  • Size: 96.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.0

File hashes

Hashes for google-cloud-pubsub-0.37.1.tar.gz
Algorithm Hash digest
SHA256 bd52eddbe752a01d4526576026c0ff83fd882b41c0b3e13ab5b9fee84ce10ffb
MD5 3b71769bbd3b834b3e8862e134cc8a12
BLAKE2b-256 9202aff8a6ba897224b26b02a2bfd1892f83ea20d12bcc71c69b1d371b235743

See more details on using hashes here.

File details

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

File metadata

  • Download URL: google_cloud_pubsub-0.37.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 99.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.0

File hashes

Hashes for google_cloud_pubsub-0.37.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 284e3667b2def13dfc221c5b15322c9638d97f5e4a20bea0afe63c06719ec829
MD5 25e8d0be90b6aa35bcd46f4411918031
BLAKE2b-256 209ac3938febb02627e4bbfb640a6ef1047dae2cac5a721821824ccf06bdebb5

See more details on using hashes here.

Supported by

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