Skip to main content

A simple task queue using Google Cloud Pub/Sub

Project description

Build Status Coverage Status PyPI Version

psq is an example Python implementation of a simple distributed task queue using Google Cloud Pub/Sub.

psq requires minimal configuration and relies on Cloud Pub/Sub to provide scalable and reliable messaging.

psq is intentionally similar to rq and simpleq, and takes some inspiration from celery and this blog post.

Installation

Install via pip:

pip install psq

Prerequisites

  • A project on the Google Developers Console.

  • The Google Cloud SDK installed locally.

  • You will need the Cloud Pub/Sub API enabled on your project. The link will walk you through enabling the API.

  • You will need to run gcloud auth before running these examples so that authentication to Google Cloud Platform services is handled transparently.

Usage

First, create a task:

def adder(a, b):
    return a + b

Then, create a pubsub client and a queue:

from gcloud import pubsub
import psq


PROJECT_ID = 'your-project-id'

client = pubsub.Client(project=PROJECT_ID)

q = psq.Queue(client)

Now you can enqueue tasks:

from tasks import adder

q.enqueue(adder)

In order to get task results, you have to configure storage:

from gcloud import pubsub
import psq


PROJECT_ID = 'your-project-id'

ps_client = pubsub.Client(project=PROJECT_ID)
ds_client = datastore.Client(project=PROJECT_ID)

q = psq.Queue(
    ps_client,
    storage=psq.DatastoreStorage(ds_client))

With storage configured, you can get the result of a task:

r = q.enqueue(adder, 5, 6)
r.result() # -> 11

You can also define multiple queues:

fast = psq.Queue(client, 'fast')
slow = psq.Queue(client, 'slow')

Things to note

Because psq is largely similar to rq, similar rules around tasks apply. You can put any Python function call on a queue, provided:

  • The function is importable by the worker. This means the __module__ that the function lives in must be importable. Notably, you can’t enqueue functions that are declared in the main module - such as tasks defined in a file that is run directly with python or via the interactive interpreter.

  • The function can be a string, but it must be the absolutely importable path to a function that the worker can import. Otherwise, the task will fail.

  • The worker and the applications queuing tasks must share exactly the same source code.

  • The function can’t depend on global context such as global variables, current_request, etc. Pass any needed context into the worker at queue time.

Delivery guarantees

Pub/sub guarantees your tasks will be delivered to the workers, but psq doesn’t presently guarantee that a task completes execution or exactly-once semantics, though it does allow you to provide your own mechanisms for this. This is similar to Celery’s default configuration.

Task completion guarantees can be provided via late ack support. Late ack is possible with Cloud Pub/sub, but it currently not implemented in this library. See CONTRIBUTING.md.

There are many approaches for exactly-once semantics, such as distributed locks. This is possible in systems such as zookeeper and redis.

Running a worker

Execute psqworker in the same directory where you tasks are defined:

psqworker.py config.q

psqworker only operates on one queue at a time. If you want a server to listen to multiple queues, use something like supervisord to run multiple psqworker processes.

Broadcast queues

A normal queue will send a single task to a single worker, spreading your tasks over all workers listening to the same queue. There are also broadcast queues, which will deliver a copy of the task to every worker. This is useful in situations where you want every worker to execute the same task, such as installing or upgrading software on every server.

broadcast_q = psq.BroadcastQueue(client)

def restart_apache_task():
    call(["apachectl", "restart"])

broadcast_q.enqueue(restart_apache_task)

Broadcast queues provide an implementation of the solution described in Reliable Task Scheduling on Google Compute Engine.

Note: broadcast queues do not currently support any form of storage and do not support return values.

Retries

Raising psq.Retry in your task will cause it to be retried.

from psq import Retry

def retry_if_fail(self):
    try:
        r = requests.get('http://some.flaky.service.com')
    except Exception as e:
        logging.error(e)
        raise Retry()

Flask & other contexts

You can bind an extra context manager to the queue.

app = Flask(__name__)

q = psq.Queue(extra_context=app.app_context)

This will ensure that the context is available in your tasks, which is useful for things such as database connections, etc.:

from flask import current_app

def flasky_task():
    backend = current_app.config['BACKEND']

Ideas for improvements

  • some sort of storage solution for broadcast queues.

  • Memcache/redis value store.

  • @task decorator that adds a delay/defer function.

  • Task chaining / groups / chords.

  • Late ack.

  • Gevent worker.

  • batch support for queueing.

Contributing changes

Licensing

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psq-0.4.0.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

psq-0.4.0-py2.py3-none-any.whl (27.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file psq-0.4.0.tar.gz.

File metadata

  • Download URL: psq-0.4.0.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for psq-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e985addc8bce9be68545fc5390fd78716255d91f804554db1a4e703f2128c572
MD5 c9a380acf452e0c17ae2b61e822f1379
BLAKE2b-256 23b44e5ad2c383c969c1eebfc894c43e5c05c8b2d29b9474bd51af702b80a28b

See more details on using hashes here.

File details

Details for the file psq-0.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for psq-0.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8dceb05438efd48a10c857359c5ed1c72e78867931a60d18ee8516c2191539e7
MD5 7a769a01d950fb14204824d50a0d7631
BLAKE2b-256 04662fe0fd9ef01ed868a8a81a042d8141b312e18eacd1e56965704c3fbe94e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page