Skip to main content

Distributed locks for Django using DB (MySQL/Postgres).

Project description

tests Test coverage status Current version on PyPi monthly downloads PyPI - Python Version PyPI - Django Version

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)

  • DATABASE_LOCKS_DEFAULT_TTL: global lock TTL value (default 10) which is ignored when lock_ttl is specified

  • DATABASE_LOCKS_DEFAULT_TTL_RENEW: number of seconds to renew lock before TTL expires (default 2)

Testing

Tox is used by the Github Action to test several python and django versions, with both MySQL and Postgres.

To quickly test locally, kick off a MySQL and/or Postgres docker container:

docker run -d –name locks-test

-p 8877:3306 -e MYSQL_ROOT_PASSWORD=root mysql:5.7

docker run -d –name locks-test-psql

-p 8878:5432 -e POSTGRES_PASSWORD=postgres postgres:10

List available environments with tox -l and then run the one you want/have:

tox -e py39-dj32-mysql # or tox -e py39-dj32-postgresql -e py39-dj32-mysql

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-database-locks-0.5.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

django_database_locks-0.5-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file django-database-locks-0.5.tar.gz.

File metadata

  • Download URL: django-database-locks-0.5.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for django-database-locks-0.5.tar.gz
Algorithm Hash digest
SHA256 97666ffcccdc7377639948d1f5f0dc63590a1313fc7848c5b080c97d2a68591a
MD5 64c8e21bffa2ac4fb21b1102e47488fc
BLAKE2b-256 aa8e44674c520b34d225f9d1edbfe56662c3109aaf49f53df32f697629aac425

See more details on using hashes here.

File details

Details for the file django_database_locks-0.5-py3-none-any.whl.

File metadata

  • Download URL: django_database_locks-0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for django_database_locks-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 1b88921becef07732c4992bac8ea5b4b514688da02c26805e8b9bf08be1b0ef2
MD5 8b6642414b18472bfbe45acdf894d3eb
BLAKE2b-256 1588e8f0317239f836c383fe0209a671e7ff7078db715cf68ffc77516bac65fa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page