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.
You can run the tests with
- Merging of master and pre-django-1.8 branches Removes management command in favor of a celery task
- Fix problem in migration to UUIDField for PostGres
- Move id to UUIDField, add code quality checks and CI
- Rename model to NonBlockingLock and add additional features
- Corrected tests and code clean-up
- Intial release.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|django_db_locking-2.0.4-py2.py3-none-any.whl (18.4 kB) Copy SHA256 Checksum SHA256||2.7||Wheel||Oct 4, 2017|
|django-db-locking-2.0.4.tar.gz (9.7 kB) Copy SHA256 Checksum SHA256||–||Source||Oct 4, 2017|