avatarcloud & nodejs tutorials

Summary

The web content provides a comprehensive guide on integrating Google Pub/Sub with Python, detailing the setup process, authentication, and code examples for both publishing and receiving messages within a Google Cloud environment.

Abstract

The article "How to Use Google Pub/Sub with Python" outlines the necessary steps to utilize Google Cloud Pub/Sub for messaging within a Python application. It begins with an introduction to Google Pub/Sub's role in modern application architecture, emphasizing its importance for distributed systems and real-time messaging. The guide then proceeds through prerequisites such as setting up a Google Cloud account, enabling the Pub/Sub API, and configuring authentication with a service account. It provides detailed instructions on creating a Google Cloud project, installing the necessary Python client library, and setting up authentication via a service account key file. The article also includes command-line interface (CLI) commands for creating topics and subscriptions, and Python code examples for publishing and receiving messages. Testing code is provided to ensure the correct functioning of message publishing and subscription. The conclusion reiterates the power of combining Google Pub/Sub with Python for efficient message exchange in applications.

Opinions

  • Google Pub/Sub is described as a pivotal component in modern application architecture, particularly for distributed systems, highlighting its role in enhancing application responsiveness and enabling event-driven functionalities.
  • The use of Python is advocated for its simplicity and versatility, making it an ideal language for interacting with Pub/Sub.
  • The guide suggests that using the gcloud CLI for project setup and configuration offers a streamlined and scriptable approach, beneficial for efficiency and automation in cloud-based operations.
  • The importance of proper authentication setup is stressed, with the recommendation to use a service account key file for secure and authenticated access to Google Cloud services.
  • The article implies that thorough testing, including unit tests for publishing and subscribing functions, is a critical step in the development process to ensure the application interacts correctly with Google Cloud Pub/Sub.

How to Use Google Pub/Sub with Python

Introduction:

Google Pub/Sub, an integral part of Google Cloud, is pivotal in modern application architecture, particularly for distributed systems. It excels in real-time messaging and event-driven functionalities, enhancing application responsiveness. This guide dives deep into the utilization of Google Pub/Sub with Python, a language known for its versatility and simplicity, making it ideal for interacting with Pub/Sub.

Prerequisites

  • Python Environment: Make sure Python is installed on your computer.
  • A Google Cloud Account: Necessary to access Google Cloud services. Create an account if you don't have one.
  • Google Cloud Project: Create a project in the Google Cloud Console and enable the Pub/Sub API.
  • Service Account and Permissions: Ensure you have a service account with pubsub.editor role.
  • Authentication Setup: Set up authentication via a service account key file.

Step 1: Set Up Your Google Cloud Project

Begin by establishing a new project in the Google Cloud Console, ensuring to enable the Pub/Sub API for messaging services. This setup is critical for leveraging the robust capabilities of Google Cloud’s messaging infrastructure. As an alternative, particularly for those preferring command-line interfaces, the gcloud CLI offers a streamlined method for project creation and configuration. Utilizing the gcloud CLI commands not only simplifies the process but also provides a flexible and scriptable approach to managing your Google Cloud resources. This method is particularly beneficial for developers and system administrators seeking efficiency and automation in their cloud-based operations.

gcloud projects create my-python-project
gcloud config set project my-python-project
gcloud services enable pubsub.googleapis.com

Step 2: Install Google Cloud Pub/Sub Client Library

Install the Python client library for Google Cloud Pub/Sub:

pip install google-cloud-pubsub

Step 3: Configure Authentication

To configure authentication, start by creating a service account in your Google Cloud project. This account will be used to grant your application the necessary permissions to interact with Google Cloud services. Once the account is created, generate a key file for it. This key file contains the credentials that your application will use to authenticate with Google Cloud. After obtaining the key file, set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable on your system to the file path of this key. This step is crucial as it allows your application to automatically use these credentials when accessing Google Cloud services, ensuring a secure and authenticated connection.

gcloud iam service-accounts create my-service-account
gcloud projects add-iam-policy-binding my-python-project --member "serviceAccount:[email protected]" --role "roles/pubsub.editor"
gcloud iam service-accounts keys create ~/keyfile.json --iam-account [email protected]
export GOOGLE_APPLICATION_CREDENTIALS=~/keyfile.json

Step 4: Create a Topic and Subscription

Utilize the gcloud CLI to efficiently create both a topic and a subscription, streamlining your setup process in Google Cloud Pub/Sub. This approach allows for quick and easy configuration of your messaging components, enhancing the communication framework within your cloud infrastructure.

gcloud pubsub topics create my-topic
gcloud pubsub subscriptions create my-subscription --topic my-topic

Step 5: Write Python Code for Pub/Sub

Publishing a Message

To publish messages, use the following Python code:

from google.cloud import pubsub_v1

publisher = pubsub_v1.PublisherClient()
topic_name = 'projects/my-python-project/topics/my-topic'

def publish_message(data):
    data = data.encode("utf-8")
    future = publisher.publish(topic_name, data)
    print(f"Published message ID: {future.result()}")

publish_message("Hello, world!")

This Python code utilizes the `google.cloud.pubsub_v1` module to publish messages to a Google Cloud Pub/Sub topic. It creates a `PublisherClient`, specifies a topic, and defines a `publish_message` function to send data. The message “Hello, world!” is encoded and published, with its ID printed on completion.

Testing Publishing Messages

Create a test file, say test_pubsub_publish.py, and add the following code:

import unittest
from unittest.mock import patch
from your_pubsub_module import publish_message

class TestPublishMessage(unittest.TestCase):
    @patch('google.cloud.pubsub_v1.PublisherClient')
    def test_publish_message(self, mock_pubsub):
        # Mock the publish method
        mock_pubsub.return_value.publish.return_value = 'mocked-message-id'

        # Call the function
        result = publish_message('Test message')

        # Assertions
        self.assertEqual(result, 'mocked-message-id')

if __name__ == '__main__':
    unittest.main()

This test checks if the publish_message function is correctly calling the Pub/Sub publish method and returns the expected result.

Receiving Messages

To receive messages, use the following Python code:

from google.cloud import pubsub_v1

subscriber = pubsub_v1.SubscriberClient()
subscription_path = 'projects/my-python-project/subscriptions/my-subscription'

def callback(message):
    print(f"Received message: {message.data}")
    message.ack()

streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print("Listening for messages...")
streaming_pull_future.result()

This Python script demonstrates how to receive messages from a Google Cloud Pub/Sub subscription using the `google.cloud.pubsub_v1` library. It begins by initializing a `SubscriberClient`, then specifies the path to a subscription. A function named `callback` is defined to handle incoming messages: it prints the message data and then acknowledges the message with `message.ack()`. This acknowledgment is crucial for informing Pub/Sub that the message has been successfully processed. The script then subscribes to the specified subscription path, passing the `callback` function as the handler for incoming messages. It prints a message indicating it’s listening and then enters a waiting state to continuously receive and process messages as they arrive.

Testing Receiving Messages

Create another test file, say test_pubsub_subscribe.py, and add the following code:

import unittest
from unittest.mock import patch, MagicMock
from your_pubsub_module import subscribe_to_messages

class TestSubscribeToMessages(unittest.TestCase):
    @patch('google.cloud.pubsub_v1.SubscriberClient')
    def test_subscribe_to_messages(self, mock_pubsub):
        # Setup a mock subscriber
        mock_subscriber = MagicMock()
        mock_pubsub.return_value = mock_subscriber

        # Call the function
        subscribe_to_messages()

        # Assertions
        mock_subscriber.subscribe.assert_called()

if __name__ == '__main__':
    unittest.main()

This test verifies that the subscribe_to_messages function correctly sets up a subscription and starts listening for messages.

Conclusion

Google Pub/Sub with Python is a powerful combination for building applications that require message exchange between components. By following the steps in this guide, you can set up Google Pub/Sub in a Python environment and start publishing and subscribing to messages.

For more comprehensive examples and advanced features, check out the Google Cloud Pub/Sub Python Client documentation.

Python
Python Programming
Gcp
Cloud
Publishing
Recommended from ReadMedium