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:
- I have one or more 'controllers' which will enqueue many tasks to be run in a PostgreSQL database.
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa4dc22a5667df4a388e7a2eefbff661d79de4b9e8db626d45fb51e9833a1ea0
|
|
| MD5 |
39124cd20be5f9db5a0e0c07b61f768d
|
|
| BLAKE2b-256 |
8da8a82f3217e759386fbd7a1781060a7a8fa69b46270e341e81d93c00c9eaf1
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
948d7df3851ecbf3622095d0ef75cce218dc8626c408e680ea7ca7379f0eb75a
|
|
| MD5 |
206b8f51ac3c2a9f45d0a382e382ba8e
|
|
| BLAKE2b-256 |
5cbea63eeab24bc06a56b42f72b2a2f8dd6f2c1c5eaa769ecfa47d1b3055979e
|