Skip to main content

A lightweight PostgreSQL-backed distributed task queue.

Project description

PostGresql Task Queue (PGTQ)

A very simple task queue system built on PostgreSQL. PGTQ was designed to solve the following use case:

  1. I have one or more 'controllers' which will enqueue many tasks to be run in a PostgreSQL database.
  2. I have one or more 'workers' which will dequeue tasks from the database and execute them.

Installation

You can install PGTQ via pip:

pip install pgtq

Usage

Here's a simple example of how to use PGTQ:

# controller.py
from pgtq import PGTQ

# Initialize the task queue
pgtq = PGTQ(dsn="postgresql://user:password@localhost/dbname")

pgtq.install() # Create necessary tables (only needed once, but it's safe to call multiple times)

# Enqueue a task
pgtq.enqueue("add_numbers", args={"a": i, "b": i * 2})
# worker.py
from pgtq import PGTQ

# Initialize the task queue
pgtq = PGTQ(dsn="postgresql://user:password@localhost/dbname")

@pgtq.task("add_numbers")
def add_numbers(a, b):
    print("Adding:", a, "+", b, "=", a + b)
    return a + b

pgtq.start_worker()

For more detailed examples, see the examples/ directory in the repository.

Batching and waiting for completion

You can group tasks together with a batch_id (UUID) and wait for them to finish before continuing:

import uuid
from pgtq import PGTQ

pgtq = PGTQ(dsn="postgresql://user:password@localhost/dbname")
pgtq.install()

batch_id = uuid.uuid4()

with pgtq.batch_enqueue(batch_id=batch_id):
    for i in range(10):
        pgtq.enqueue("add_numbers", args={"a": i, "b": i * 2})

# blocks until no queued/in-progress tasks remain for this batch
pgtq.wait_for_batch(batch_id)

Async users can call AsyncPGTQ.wait_for_batch(batch_id) in the same way.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

pgtq-0.3.1.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgtq-0.3.1-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file pgtq-0.3.1.tar.gz.

File metadata

  • Download URL: pgtq-0.3.1.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for pgtq-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b72815e0d646dcb7b9d293474934ac76665cc6bc3084805dfab4e5ff0dd48691
MD5 689255ad7bb16ef068712eb2569d68a6
BLAKE2b-256 272d552219f575a01b40f913d8a835fbd956bf8baf8f9dbc207637f8f73656fa

See more details on using hashes here.

File details

Details for the file pgtq-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pgtq-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for pgtq-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 765c053b22271b7ac3b31780727068cb8cb8a0645e6a5ccfbf726d8d4361046e
MD5 00614eda1541e60b0d5145dc53cca283
BLAKE2b-256 07959ff270b95283d8aef073c763f64dc883df94aa2f7ce8a5952101d2980815

See more details on using hashes here.

Supported by

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