Concurrency guard for Django using PostgreSQL advisory locks.
Prevent race conditions in critical sections using simple, expressive decorators or context managers.
Race conditions are easy to introduce and hard to detect.
Example:
def withdraw(user, amount):
if user.balance >= amount:
user.balance -= amount
user.save()Two concurrent requests can both pass the balance check and withdraw twice.
This library prevents that.
- PostgreSQL advisory lock backend
- Simple decorator API
- Context manager support
- Business-key locking (not limited to database rows)
- Timeout and conflict handling
pip install django-concurrency-safeImport:
from concurrency_safe import concurrency_safe, lock, LockAcquireTimeout@concurrency_safe(key="withdraw:user:{user_id}")
def withdraw(user_id, amount):
...Only one execution per key runs at a time.
with lock("stock:ABC"):
process_order()When the lock cannot be acquired:
@concurrency_safe(key="stock:{sku}")Raises LockAcquireTimeout by default.
Custom handler:
from django.http import JsonResponse
def busy(*args, **kwargs):
return JsonResponse({"detail": "busy"}, status=409)
@concurrency_safe(
key="stock:{sku}",
on_conflict=busy,
)django-concurrency-safe is tested against real PostgreSQL using advisory locks.
Integration tests require a running PostgreSQL instance.
Start PostgreSQL using docker compose:
docker compose up -dRun tests:
export DATABASE_URL=postgres://postgres:postgres@localhost:5432/concurrency_safe
pytestTests will automatically use PostgreSQL via the DATABASE_URL environment variable.
example/ contains a runnable Django demo showcasing the race condition, row-level locks, and advisory locks (PostgreSQL).
Unlike row-level locking, advisory locks:
- Work without locking a specific database row
- Support arbitrary business keys
- Are fast and lightweight
- Automatically release on connection close
- Python 3.10+
- Django 4.2+
- PostgreSQL
- Redis backend
- Async support
- Metrics hooks
MIT