Skip to main content

django-concurrency-safe

PyPI version Python versions

Concurrency guard for Django using PostgreSQL advisory locks.

Prevent race conditions in critical sections using simple, expressive decorators or context managers.


Why?

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.


Features

  • PostgreSQL advisory lock backend
  • Simple decorator API
  • Context manager support
  • Business-key locking (not limited to database rows)
  • Timeout and conflict handling

Installation

pip install django-concurrency-safe

Quickstart

Import:

from concurrency_safe import concurrency_safe, lock, LockAcquireTimeout

Using the decorator

@concurrency_safe(key="withdraw:user:{user_id}")
def withdraw(user_id, amount):
    ...

Only one execution per key runs at a time.

Using the context manager

with lock("stock:ABC"):
    process_order()

Conflict handling

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,
)

Testing

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 -d

Run tests:

export DATABASE_URL=postgres://postgres:postgres@localhost:5432/concurrency_safe
pytest

Tests will automatically use PostgreSQL via the DATABASE_URL environment variable.


Example project

example/ contains a runnable Django demo showcasing the race condition, row-level locks, and advisory locks (PostgreSQL).


Why advisory locks?

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

Requirements

  • Python 3.10+
  • Django 4.2+
  • PostgreSQL

Roadmap

  • Redis backend
  • Async support
  • Metrics hooks

License

MIT

Metadata

Release files for django-concurrency-safe 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-concurrency-safe 0.0.2
File Size Uploaded
django_concurrency_safe-0.0.2.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-concurrency-safe 0.0.2
File Interpreter ABI Platform
django_concurrency_safe-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 21.9 kB

Release files / django_concurrency_safe-0.0.2.tar.gz

Download URL django_concurrency_safe-0.0.2.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
424ca2d8135632d0aaaa21131a95f8769bce39b500ec9fb01f0b8ece730cf78a
BLAKE2b-256 checksum
How to use checksums
3ba15810b1b14501e3a91da581fcd32a8b7643bda409ab2f1a6d5ea3e81d337c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release files / django_concurrency_safe-0.0.2-py3-none-any.whl

Download URL django_concurrency_safe-0.0.2-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
01b60f0fb479b449d7e86310baf3a5c6d2f97f89f5c7a5073e5a1c45a89350ef
BLAKE2b-256 checksum
How to use checksums
fbd6d2a955dc4d7f72a574deafed6a9d906bd50668b60115d8488f15cf1e6cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.0

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page