Skip to main content

Distributed locks for Django using Zookeeper.

Project description

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

Distributed locks for Django using Zookeeper

Installation

pip install django-zk-locks

Usage

django-zk-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

There are also the following options you can specify in the project settings.py

  • ZOOKEEPER_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.

  • ZOOKEEPER_LOCKS_ENABLED: set to False to globally disable locks (default True)

  • ZOOKEEPER_LOCKS_HOSTS: connection string for Zookeeper, such as “localhost:2181,localhost:2182,localhost:2183”

Testing

Tox is used by the Github Action to test several python and django versions.

To quickly test locally, kick off a Zookeeper docker container:

docker run -d –name locks-test

-p 2181:2181 zookeeper:3.9

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

tox -e py310-dj42

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_zk_locks-0.0.1.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

django_zk_locks-0.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file django_zk_locks-0.0.1.tar.gz.

File metadata

  • Download URL: django_zk_locks-0.0.1.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for django_zk_locks-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d7d9ce9fa2adae190f6edc68f2963e12079c8eb8756d69910e074f24963938a6
MD5 d5330819f3e16221329a17ba52e8c030
BLAKE2b-256 8976def2aeb4eaa6b48b61ca7a25afc95e7838ae234e83cd4bdb3c63122d239c

See more details on using hashes here.

File details

Details for the file django_zk_locks-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_zk_locks-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8f3b2a6d08bcdeea1c43dab012eccf9f9c3641e66a079e87003ab06972d2aab
MD5 c3a2a938531fb673e4056de7a7d1bae9
BLAKE2b-256 0edc95322a4efb8311f21416d436d2af2bb92cb9af97acfd5811f264fc95d670

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