0
0
GcpHow-ToBeginner · 4 min read

How to Use Google Cloud Pub/Sub with Python: Simple Guide

Use the google-cloud-pubsub Python library to create a publisher and subscriber client. Publish messages with publish() and receive them by creating a subscription with subscribe().
📐

Syntax

To use Pub/Sub in Python, you first create a PublisherClient to send messages and a SubscriberClient to receive messages. You publish messages to a topic_path and listen for messages on a subscription_path.

  • PublisherClient.publish(topic_path, data): Sends a message to the topic.
  • SubscriberClient.subscribe(subscription_path, callback): Listens for messages and runs the callback on each.
python
from google.cloud import pubsub_v1

# Create clients
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()

# Define paths
project_id = "your-project-id"
topic_id = "your-topic"
subscription_id = "your-subscription"

topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)

# Publish message
future = publisher.publish(topic_path, b"Hello, Pub/Sub!")
print(f"Published message ID: {future.result()}")

# Define callback for subscriber
def callback(message):
    print(f"Received message: {message.data.decode('utf-8')}")
    message.ack()

# Subscribe to messages
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}...")
💻

Example

This example shows how to publish a message to a topic and receive it with a subscriber callback. It prints the published message ID and the received message content.

python
from google.cloud import pubsub_v1
import time

project_id = "your-project-id"
topic_id = "test-topic"
subscription_id = "test-subscription"

publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()

topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)

# Publish a message
future = publisher.publish(topic_path, b"Hello from Python Pub/Sub!")
print(f"Published message ID: {future.result()}")

# Callback to process messages

def callback(message):
    print(f"Received message: {message.data.decode('utf-8')}")
    message.ack()

# Start subscriber
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}...")

# Keep the main thread alive to listen for messages
try:
    time.sleep(10)
except KeyboardInterrupt:
    streaming_pull_future.cancel()
Output
Published message ID: 1234567890123456 Listening for messages on projects/your-project-id/subscriptions/test-subscription... Received message: Hello from Python Pub/Sub!
⚠️

Common Pitfalls

Common mistakes when using Pub/Sub with Python include:

  • Not creating the topic or subscription in Google Cloud before running the code.
  • Forgetting to ack() the message in the callback, causing messages to be redelivered.
  • Using blocking code that stops the subscriber from listening continuously.
  • Not setting correct permissions for the service account running the code.

Always ensure your topic and subscription exist and your callback acknowledges messages.

python
from google.cloud import pubsub_v1

# Wrong: Not acknowledging message
# def callback(message):
#     print(f"Received: {message.data.decode('utf-8')}")
#     # message.ack() missing

# Right: Acknowledge message
# def callback(message):
#     print(f"Received: {message.data.decode('utf-8')}")
#     message.ack()
📊

Quick Reference

Remember these key steps when using Pub/Sub with Python:

  • Install the library: pip install google-cloud-pubsub
  • Create PublisherClient and SubscriberClient
  • Publish messages with publish()
  • Subscribe with subscribe() and handle messages in a callback
  • Always acknowledge messages with message.ack()

Key Takeaways

Use google-cloud-pubsub library to interact with Pub/Sub in Python.
Publish messages with PublisherClient.publish() and subscribe with SubscriberClient.subscribe().
Always acknowledge messages in the subscriber callback to avoid redelivery.
Ensure topics and subscriptions exist in your Google Cloud project before running code.
Keep the subscriber running to continuously listen for messages.