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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c565bfdf155798d13933f03eee4f73b989e27261071920e8e1a2e09eaa933a07 |
|
MD5 | 13cc796917c3873039a6e3753107c89f |
|
BLAKE2b-256 | f41a9ed2d0bf6e3b9fbdf049861f29ce86ba61c94ca2e975c4d8104597f1c90f |
Hashes for django_database_locks-0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f56d51d826d3168f48b0014987aa50f3a8cdafc06e79e1eba29c279e2530904 |
|
MD5 | c44bc61e34b57997f80f79089d7e4d17 |
|
BLAKE2b-256 | e6ced6c76866f6f234019fd077c332b4248f5b5b3f16db8c16ba838ca6458f2d |