Skip to main content

A locking system for Django apps simply using the database.

Project description

Privex's Django Database Lock Manager

Build Status Codecov PyPi Version License Button PyPI - Downloads PyPI - Python Version PyPI - Django Version GitHub last commit

+===================================================+
|                 © 2019 Privex Inc.                |
|               https://www.privex.io               |
+===================================================+
|                                                   |
|        Django Database Lock Manager               |
|        License: X11/MIT                           |
|                                                   |
|        Core Developer(s):                         |
|                                                   |
|          (+)  Chris (@someguy123) [Privex]        |
|                                                   |
+===================================================+

Django Database Lock Manager - Easy to use lock system using your Django app's database
Copyright (c) 2019    Privex Inc. ( https://www.privex.io )

Install with pip

We recommend at least Python 3.6 - we cannot guarantee compatibility with older versions.

pip3 install django-lockmgr

Add lockmgr to your INSTALLED_APPS

INSTALLED_APPS = [
    'django.contrib.admin.apps.SimpleAdminConfig',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    # ...
    'lockmgr'
]   

Run the migrations

./manage.py migrate lockmgr

Usage

Use the LockMgr in your code like so:

from lockmgr.lockmgr import LockMgr, Locked

try:
    with LockMgr('mylock') as lck:
        print('The lock "mylock" is now locked.')
        lck.lock('otherlock')
        print('The lock "otherlock" should also be locked.')
    print('The locks "mylock" and "otherlock" should now both be cleared.')
except Locked as e:
    print('Error! mylock is already locked: ', type(e), str(e))

If you want to wait for the lock to be released, rather than immediately excepting:

from lockmgr.lockmgr import LockMgr, Locked

try:
    # expires=60 means the lock will expire after 60 seconds if you don't renew it.
    # wait=90 (must be in 5 second intervals) means: if the key is locked, retry every 5 seconds, if 90 seconds have 
    #         passed and it's still locked, then give up and raise Locked.
    with LockMgr('somelock', expires=60, wait=90) as lck:
        print('The lock "somelock" is now locked.')
    print('The lock "somelock" should now be cleared.')
except Locked as e:
    print('Error! After retrying for 90 seconds, "somelock" is still locked: ', type(e), str(e))

Unit Tests

To run the unit tests, clone the project and make a .env file containing details for a database:

DB_BACKEND=mysql
DB_NAME=lockmgr
DB_USER=someuser
DB_PASS=mypassword

Install all required dependencies:

pip3 install -r requirements.txt -U

Now run the tests (--verbose for more detailed testing output):

./manage.py test --verbose

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-lockmgr-1.1.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_lockmgr-1.1.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file django-lockmgr-1.1.1.tar.gz.

File metadata

  • Download URL: django-lockmgr-1.1.1.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for django-lockmgr-1.1.1.tar.gz
Algorithm Hash digest
SHA256 509249cebc845d19baec173bdbe1c7aa4e919ba81ef52b40c7f6fe2e0f47201e
MD5 dc7a81503a682d6c45d90719a3246e0f
BLAKE2b-256 ba4e298a7d7e5189457a2a6363e61214ef148fb2c5046c45fa9e07bbb94451f5

See more details on using hashes here.

File details

Details for the file django_lockmgr-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_lockmgr-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for django_lockmgr-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cff03b49d30ff774a2a525eef1fe764330fdcbda4bbfc0f2c400722500149237
MD5 ed955ea684fad00e1648d42e9743994c
BLAKE2b-256 3d2a1d361a78d0f1bd6058ce406e6eed3bacba3c56987bd7e842038df261ce95

See more details on using hashes here.

Supported by

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