Distributed locks for Django using DB (MySQL/Postgres).
Project description
Distributed locks for Django using DB (MySQL/Postgres)
Given the limitation that Percona Cluster does not support MySQL locks, this app implements locks using select_for_update() (row locks).
Installation
pip install django-database-locks
Usage
django-database-locks exposes one single the lock contextmanager and the locked decorator.
The locked decorator will wrap a django management command (subclasses of django.core.management.base.BaseCommand) or any function with the lock contextmanager:
from django.core.management.base import BaseCommand
from database_locks import locked
@locked
class Command(BaseCommand):
...
def handle(self, *args, **options):
self.stdout.write('Got the lock')
from database_locks import locked
class SomeClass:
def non_locked(self):
pass
@locked
def locked(self):
print('got lock')
from database_locks import lock
class SomeClass:
def non_locked(self):
pass
def locked(self):
with lock():
print('got lock')
Docs
Both lock and locked have the same optional args:
:param lock_name: unique name in DB for this function
:param timeout: numbers of seconds to wait to acquire lock
:param lock_ttl: expiration timer of the lock, in seconds (set to None to infinite)
:param locked_by: owner id for the lock (if lock is active but owner is the same, returns acquired)
:param auto_renew: if set to True will re-acquire lock (for `lock_ttl` seconds) before `lock_ttl` is over.
auto_renew thread will raise KeyboardInterrupt on the main thread in case re-acquiring fails
:param retry: retry every `retry` seconds acquiring until successful. set to None or 0 to disable.
:param lost_lock_cb: callback function when lock is lost (when re-acquiring). defaults to raising LockException
There are also the following options you can specify in the project settings.py
DATABASE_LOCKS_STATUS_FILE: file that will be updated with the lock status (default None). Useful when you have multiple shared-lock processes, to quickly inspect which one has the lock.
DATABASE_LOCKS_ENABLED: set to False to globally disable locks (default True)
Project details
Release history Release notifications | RSS feed
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
Hashes for django-database-locks-0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9093a96ecb3d0bf90dd392194bb1ac7aca5509267515116f5224965c72f94edf |
|
MD5 | 1403fd5a6403274b3d0091c607b67bcc |
|
BLAKE2b-256 | b4ecd0e84d1d32fc124df432fa676decd044b1a6b567565cef7865a04e43d0e0 |
Hashes for django_database_locks-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f76f9dd7606023ec2481983c53d7ebc07f479af36516da09c44aa0aeeb2a12f |
|
MD5 | ce4cfc3dd13d83bae81b227e9aa3209d |
|
BLAKE2b-256 | 85442d6fac2e0129b72f2d8d860864d4fd89d0af2c95dfffbf47b9f3b28e7c09 |