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.0.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.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pgtq-0.3.0.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.0.tar.gz
Algorithm Hash digest
SHA256 4f3c8398620b5294184393169e3758bea055f9c4b30770e10c7381dc403bfa64
MD5 eaeee5f75976ec24e56448aa06f59165
BLAKE2b-256 b74b263d227b554939d44018596f2e595496c32f7650f8f9fed72e7030adf69a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgtq-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23ba7bbec7484fa9e4884723eb4a3441bc18bca7a2ccfb8eb2a3df307aa29d19
MD5 07925be559485df70961320a6c99f636
BLAKE2b-256 e001e77ed1c0bdda8a6662e1088143ab3b6f82818f4a634267378a0d5fe473af

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