Distributed Tasks Processing With Google Pub/Sub
What is Pub/Sub?
Pub/Sub is an asynchronous messaging service that decouples services that produce events from services that process events. You can use Pub/Sub as messaging-oriented middleware or event ingestion and delivery for streaming analytics pipelines. Pub/Sub offers durable message storage and real-time message delivery with high availability and consistent performance at scale. Pub/Sub servers run in all Google Cloud regions around the world.
Core concepts
- Topic: A named resource to which messages are sent by publishers.
- Subscription: A named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application.
- Message: The combination of data and (optional) attributes that a publisher sends to a topic and is eventually delivered to subscribers.
- Message attribute: A key-value pair that a publisher can define for a message. For example, key iana.org/language_tag and value en could be added to messages to mark them as readable by an English-speaking subscriber.
Install
pip install google-cloud-pubsub |
Task Dispatcher (Publisher)
In this example, the publisher will act as a task distributer.
import os |
Task Workers (Subscribers)
Multiple workers will subscribe to a same subscriptions to receive difference tasks from a same publisher.
import os |