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.4.1.tar.gz (17.7 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.4.1-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pgtq-0.4.1.tar.gz
Algorithm Hash digest
SHA256 fa4dc22a5667df4a388e7a2eefbff661d79de4b9e8db626d45fb51e9833a1ea0
MD5 39124cd20be5f9db5a0e0c07b61f768d
BLAKE2b-256 8da8a82f3217e759386fbd7a1781060a7a8fa69b46270e341e81d93c00c9eaf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pgtq-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 14.7 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.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 948d7df3851ecbf3622095d0ef75cce218dc8626c408e680ea7ca7379f0eb75a
MD5 206b8f51ac3c2a9f45d0a382e382ba8e
BLAKE2b-256 5cbea63eeab24bc06a56b42f72b2a2f8dd6f2c1c5eaa769ecfa47d1b3055979e

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