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.5.0.tar.gz (18.3 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.5.0-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pgtq-0.5.0.tar.gz
Algorithm Hash digest
SHA256 cbcdd66fb9aa0ed3c68784dd40bf429aa3e96f281b25974b45687df44fbea14d
MD5 baeff3f4601f67bb9928e8825f432c14
BLAKE2b-256 dae63dfca5624edf8e577a2678df92f46a469a8aa19274fa67029fe4099e0a9b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgtq-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 14.8 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.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 616b5122f97d54b91cb9b3a51b2ab8eee47f8c282f97774950e5f10dece024b1
MD5 0b8fcdeb8df2b00d39f8a723d313df53
BLAKE2b-256 53ea71f9c470dcff8a44a0cf7115def9d89cffc5e48ed810ec2ecb0b47cb0512

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