GCP Pub/Sub DAO
The library provides DAO classes for GCP pubsub publisher/subscriber.
Installation
pip install gcp-pubsub-dao
Usage
- sync subscriber:
from gcp_pubsub_dao import PubSubSubscriberDAO, Message
dao = PubSubSubscriberDAO(project_id="prodect-dev", subscription_id="subscription")
messages: Message = dao.get_messages(messages_count=2)
for message in messages:
print(message.data)
dao.ack_messages(ack_ids=[message[0].ack_id])
dao.nack_messages(ack_ids=[message[1].ack_id])
dao.close() # to clean up connections
- sync publisher:
from gcp_pubsub_dao import PubSubPublisherDAO
dao = PubSubPublisherDAO(project_id="prodect-dev")
try:
dao.publish_message(topic_name="topic", payload=b"asdfsdf", attributes={"kitId": "AW12345678"})
except Exception as ex:
print(ex)
- async subscriber:
from gcp_pubsub_dao import AsyncPubSubSubscriberDAO, Message
dao = AsyncPubSubSubscriberDAO(project_id="prodect-dev", subscription_id="subscription")
messages: Message = await dao.get_messages(messages_count=2)
for message in messages:
print(message.data)
await dao.ack_messages(ack_ids=[message[0].ack_id])
await dao.nack_messages(ack_ids=[message[1].ack_id])
- async publisher:
from gcp_pubsub_dao import AsyncPubSubPublisherDAO
dao = AsyncPubSubPublisherDAO(project_id="prodect-dev")
try:
await dao.publish_message(topic_name="topic", payload=b"asdfsdf", attributes={"kitId": "AW12345678"})
except Exception as ex:
print(ex)
- async worker pool
import asyncio
import sys
sys.path.append("./")
from gcp_pubsub_dao import AsyncPubSubSubscriberDAO
from gcp_pubsub_dao.worker_pool import WorkerPool, WorkerTask, HandlerResult
from gcp_pubsub_dao.entities import Message
async def handler1(message: Message):
print(f"handler1: {message}")
await asyncio.sleep(2)
return HandlerResult(ack_id=message.ack_id, is_success=True)
async def handler2(message: Message):
print(f"handler2: {message}")
await asyncio.sleep(5)
return HandlerResult(ack_id=message.ack_id, is_success=True)
def heartbeat_func():
print("Heartbeat: Worker is alive")
async def main():
tasks = [
WorkerTask(
subscriber_dao=AsyncPubSubSubscriberDAO(project_id="ash-dev-273120", subscription_id="http-sender-sub"),
handler=handler1,
),
WorkerTask(
subscriber_dao=AsyncPubSubSubscriberDAO(project_id="ash-dev-273120", subscription_id="email-sender-sub"),
handler=handler2,
),
]
# Create worker pool with heartbeat function
wp = WorkerPool(heartbeat_func=heartbeat_func)
# Run in async mode (default) - all tasks run concurrently
await wp.run(tasks=tasks)
# Or run in sync mode - tasks run one by one in order
# await wp.run(tasks=tasks, mode="sync")
if __name__ == "__main__":
asyncio.run(main())
Worker Pool Features
The WorkerPool provides two execution modes:
Async Mode (default)
- All tasks run concurrently using
asyncio.TaskGroup - Tasks can execute in any order or simultaneously
- Best for independent tasks that don't need to be processed in sequence
Sync Mode
- Tasks run one by one in the order they are provided
- Each task completes before the next one starts
- Useful when tasks need to be processed in a specific sequence
- Note: Message processing within each task is still asynchronous
Heartbeat Function
- Optional callback function that gets called during worker execution
- Useful for monitoring worker health and activity
- Called before processing messages in each iteration
- Can be used for logging, metrics, or health checks
WorkerTask Configuration
subscriber_dao: The async subscriber DAO instancehandler: Async function that processes messages and returnsHandlerResultbatch_size: Number of messages to fetch per batch (default: 10)return_immediately: Whether to return immediately if no messages (default: False)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
gcp_pubsub_dao-0.5.0.tar.gz
(6.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gcp_pubsub_dao-0.5.0.tar.gz.
File metadata
- Download URL: gcp_pubsub_dao-0.5.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/1.6.1 CPython/3.11.4 Linux/6.17.0-1022-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cff2d97524e988c3831200a170a6ee2055cf8039fe5e9c40b9faf0965f53ae80
|
|
| MD5 |
287898096f9f4f2122cc5866b71dc401
|
|
| BLAKE2b-256 |
f5b0446f5b59eff51e66692070fab106c744379418699edd0774fbfd37ab96be
|
File details
Details for the file gcp_pubsub_dao-0.5.0-py3-none-any.whl.
File metadata
- Download URL: gcp_pubsub_dao-0.5.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
poetry/1.6.1 CPython/3.11.4 Linux/6.17.0-1022-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8d377184b19fc96458e9c88b69c2aa49cb253e2e6f67a5f207ef880fc200204
|
|
| MD5 |
16262f444e3ac99c09f33e79a1d0c256
|
|
| BLAKE2b-256 |
687eb863e0e897be3fd38a74c12fa96d57e2ac1ceabf2083ab95697237f20bc6
|