tinyjobs: a django.tasks backend and worker on SQLite, Postgres or Redis

Hi everyone,

Django 6 ships django.tasks, a standard API for background tasks, but leaves the worker to third parties. I’ve been building tinyjobs, an open source job queue for Python, and it now includes a django.tasks backend plus the worker to run it.

The idea is that you don’t need a broker to start: the queue is a SQLite file. When you outgrow one machine you switch the URL to Redis and your task code stays the same.

# settings.py
INSTALLED_APPS = [..., "tinyjobs.contrib.django"]

TASKS = {
    "default": {
        "BACKEND": "tinyjobs.contrib.django.TinyJobsBackend",
        "OPTIONS": {"url": "sqlite:///jobs.db"},
    }
}
# shop/tasks.py
from django.tasks import task

@task
def send_receipt(order_id: int) -> None:
    ...

send_receipt.enqueue(42)
python manage.py tinyjobs worker

What works today: queues, priorities, run_after, async tasks, takes_context, get_result(), and the task_enqueued / task_started / task_finished signals. manage.py tinyjobs also gives you jobs, stats and check for inspecting the queue and for health probes.

A few design choices you may care about:

  • Tasks are resolved by import path, and only inside your INSTALLED_APPS, so a job can never make the worker import code your project doesn’t already trust.
  • Arguments are JSON, as django.tasks expects; there’s no pickle.
  • Delivery is at-least-once: if a worker dies mid-task, its lease expires and the task is marked FAILED, since Django tasks aren’t retried.
  • The queue lives outside your database, so an enqueue can’t share your transaction. Use transaction.on_commit. If you need transactional enqueue through the ORM today, django-tasks-db is the better fit.

I tested it with a real project under uvicorn --workers 2 and under runserver: 1,000 requests across sync and async views, each enqueuing a task that writes a row, with two workers on SQLite and then on Redis. Every task ran exactly once. Enqueueing through django.tasks costs about the same as calling tinyjobs directly, around 6,000 per second.

I’d really appreciate feedback, especially from anyone who has been following django.tasks: what you’d expect from a backend, and anything that doesn’t feel Django-like.

2 Likes

tinyjobs 0.8.0 is out, and it adds a Postgres backend, which is probably what most Django projects already run.

What changes for Django users:

  • Transactional enqueue. With "database": "default" in OPTIONS, every enqueue joins that database’s transaction: a task enqueued inside transaction.atomic() exists only if the block commits, so no more transaction.on_commit wrappers. This corrects what I wrote above about enqueues not joining transactions; that still holds for SQLite and Redis.
  • No polling wait. An idle worker is woken by LISTEN/NOTIFY and starts a new job in a few milliseconds.
  • Server clock. Every machine keeps Postgres’ time, so workers on different machines don’t need their clocks to agree.
  • Same backend contract as SQLite and Redis: queues, priorities, run_after, async tasks, takes_context, get_result() and the task_* signals all work, and switching is just the URL.
TASKS = {
    "default": {
        "BACKEND": "tinyjobs.contrib.django.TinyJobsBackend",
        "OPTIONS": {"url": "postgresql://...", "database": "default"},
    }
}

Measured on Postgres 17 on a laptop: 20,000 jobs over four workers in 2.1 seconds, each run exactly once, and 6,850 enqueues a second from 24 threads. Twelve SIGKILLs on workers during 4,000 jobs, and three Postgres restarts during 6,000, left nothing lost and nothing run twice.

Feedback is still very welcome, especially from anyone already using django.tasks on Postgres.

tinyjobs 0.9.0 is out, and this one is about free-threaded Python.

On 3.14t the worker keeps the GIL off, so def tasks, which is what most Django tasks are, run in parallel in the worker’s thread pool. The worker also takes --loops N to run several event loops in threads inside one process, each with its own Postgres connection pool:

python manage.py tinyjobs worker --loops 3

Measured on a 4-core VPS against Postgres 17 (no CPU cap, async commits), draining 80,000 no-op def tasks: one process with 3 loops does 1,750 jobs/s with the GIL and 2,250 without, close to what 3 separate processes do (2,640 and 2,980), in a third of the memory. On tasks that burn about 1 ms of CPU each, the usual Django case, one process goes from 870 jobs/s with the GIL to 1,230 without, and adding loops with the GIL on makes it worse (500) while without it holds (1,190). Postgres itself took about 0.8 ms of CPU per job, more than twice what the worker spent, so on Postgres the store is the ceiling well before Python is; on Redis the same worker reaches 7,100 jobs/s.

loops works on Postgres and Redis; SQLite refuses it above 1, since its single writer would make the loops wait on each other, so run more processes there. Two install notes for 3.14t: psycopg-binary has no free-threaded wheels, so install psycopg psycopg-pool against the system libpq; and hiredis turns the GIL back on as soon as redis-py imports it, so leave it out (the worker logs a warning if it finds the GIL on).

Full numbers, with and without the GIL, in the README.