Skip to content

mathpresso/django-db-locking

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Pieter De Decker
Dec 8, 2017
def80d2 · Dec 8, 2017
Dec 8, 2017
Dec 7, 2017
May 19, 2017
May 19, 2017
May 19, 2017
Jun 9, 2017
Dec 8, 2017
Aug 4, 2017
Jun 9, 2017
Jun 9, 2017
May 19, 2017
May 24, 2017
May 19, 2017

Repository files navigation

Django-locking

https://coveralls.io/repos/github/vikingco/django-db-locking/badge.svg?branch=master https://travis-ci.org/vikingco/django-db-locking.svg?branch=master

Usage

The simplest use is by using it as a context manager:

with NonBlockingLock.objects.acquire_lock(obj=model_obj):
    model_obj.do_something()

Or you can keep track of the lock yourself:

try:
    lock = NonBlockingLock.objects.acquire_lock(obj=model_obj)
except AlreadyLocked:
    return False

model_obj.do_something()
lock.release()

If you have no Django model, or you want to be able to specify the lock name yourself, you can do that too:

# this will raise AlreadyLocked, if it's locked
lock = NonBlockingLock.objects.acquire_lock(lock_name='my_lock')
do_something()
lock.release()

Note that locks can expire automatically. There is a LOCK_MAX_AGE settings where you can specify a default lock release value for locks in your entire Django codebase. This value can be overridden per lock by setting the max_age parameter.

Test

You can run the tests with

tox

Releases

v2.0.0:
Merging of master and pre-django-1.8 branches Removes management command in favor of a celery task
v1.2.1:
Fix problem in migration to UUIDField for PostGres
v1.2.0:
Move id to UUIDField, add code quality checks and CI
v1.1.0:
Rename model to NonBlockingLock and add additional features
v1.0.1:
Corrected tests and code clean-up
v1.0.0:
Intial release.

About

Use the Django ORM to lock objects in the database

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%