Python Client for Google Cloud Pub/Sub using `aiohttp` for asynchronous operations. It is part of the `gcloud-aio` monorepo, which provides async clients for various Google Cloud services. The library is actively maintained with regular updates across its sub-packages.
Install & Compatibility
Where this runs
tested against v6.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 60.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 5.8s · import 0.000s · 63MB
62MB installed
● package 62MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
from gcloud.aio.pubsub import Publisher
This quickstart demonstrates how to publish a message to a Pub/Sub topic and then subscribe to a subscription to receive and acknowledge a message using `gcloud-aio-pubsub`. It assumes you have `GCP_PROJECT` and `GCP_CREDENTIALS` environment variables set up, and that the specified topic and subscription already exist in your Google Cloud Project.
import asyncio
import json
import os
from aiohttp import ClientSession
from gcloud.aio.pubsub import Publisher, Subscriber
async def main():
# Ensure GCP_PROJECT and GCP_CREDENTIALS are set as environment variables.
# For local testing, GCP_CREDENTIALS should be the JSON key file content.
# Example: export GCP_CREDENTIALS=$(cat /path/to/keyfile.json)
project = os.environ.get('GCP_PROJECT', '')
if not project:
print("Error: GCP_PROJECT environment variable not set.")
return
topic_name = 'my-test-topic' # Replace with an existing topic
subscription_name = 'my-test-subscription' # Replace with an existing subscription
async with ClientSession() as session:
# --- Publisher Example ---
publisher = Publisher(session=session, project=project)
data = {'message': 'Hello, gcloud-aio-pubsub!'}
message_id = await publisher.publish(topic_name, [data])
print(f"Published message to '{topic_name}' with ID: {message_id}")
# --- Subscriber Example ---
subscriber = Subscriber(session=session, project=project)
print(f"Listening for messages on '{subscription_name}'...")
try:
# Fetch up to 1 message for this example
async for message in subscriber.subscribe(subscription_name, max_messages=1):
print(f"Received message: {message.data.decode('utf-8')}")
await message.ack()
print("Message acknowledged.")
break # Exit after first message for this quickstart
except Exception as e:
print(f"Error during subscription: {e}")
if __name__ == '__main__':
asyncio.run(main())
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.