Registry / database / django-pglocks

django-pglocks

JSON →
library1.0.4pypypi✓ verified 86d ago

Provides useful context managers for PostgreSQL advisory locks in Django. Version 1.0.4 (latest) released Aug 2021. Low maintenance, stable, no major changes expected.

pip install django-pglocks
INSTALL
IMPORT
SIG · DJANGO-PGLOCKS
D
django-pglocks
databasepythonv1.0.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

advisory_lock
from django_pglocks import advisory_lock
from django_pglocks import advisory_lock as lock
Renaming is fine but not needed; just use standard import.

Acquire a PostgreSQL advisory lock using a string key. The lock is released when the context exits.

from django_pglocks import advisory_lock with advisory_lock('my_lock_key'): # critical section pass
Debug
Known issues
gotchaAdvisory locks are not automatically released on transaction rollback; ensure the context manager exits properly.
fix
Always use the context manager; avoid manual acquire/release.
affects: all
gotchaLock key collisions: The library hashes strings to 64-bit integers; different strings may collide (hash collision).
fix
Use unique, descriptive key strings; consider prepending a namespace.
affects: all
gotchaPostgreSQL advisory locks are session-level; closing the connection releases all locks. Ensure connection persistence inside the critical section.
fix
Keep the same database connection active within the lock context.
affects: all
Errors
Common errors & fixes
AttributeError: 'NoneType' object has no attribute 'cursor'
advisory_lock called outside of a database connection context (e.g., before Django is fully configured).
fix
Ensure the lock is acquired inside a view or management command where a DB connection is available.
OperationalError: advisory lock already held by this session
Trying to acquire the same lock twice in the same session without releasing it (e.g., nested locks with same key).
fix
Use different keys for nested locks or avoid nesting same-key locks.
ModuleNotFoundError: No module named 'django_pglocks'
Library not installed in current environment.
fix
Run `pip install django-pglocks`.
Upgrade
Version history
1.0.4latest on PyPI · released Dec 26, 2019
Audit
Dependencies
djangorequiredCore dependency; operates within Django's ORM and database connections.
psycopg2requiredRequired for PostgreSQL support; uses psycopg2 features.
Agent activity
6 hits · last 30 days
node
6
Resources
django-pglocks — pip install django-pglocks · libregistry