Using the lockd API
lockd provides a light-weight implementation of a locking mechanism for long-running resources by maintaining lock files in a given directory.
>>> import tempfile >>> lockdir = tempfile.mkdtemp() >>> from gocept.lockd.lockd import Lockd >>> daemon = Lockd(lockdir)
Resources are identified by a string and clients performing the lock are identified by another string:
>>> daemon.lock('resource1', 'client1')
Once a resource is locked, it can not be locked again:
>>> daemon.lock('resource1', 'client2')
Traceback (most recent call last):
Exception: Resource already locked by 'client1'
However, other resources can be locked in parallel:
>>> daemon.lock('resource2', 'client2')
Clients, other than the client who locked a resource, can not unlock it:
>>> daemon.unlock('resource1', 'client2')
Traceback (most recent call last):
Exception: Resource locked by 'client1' cannot be unlocked by 'client2'
The client, who locked the resource, can unlock it again:
>>> daemon.unlock('resource1', 'client1')
Once unlocked, it can not be unlocked a second time:
>>> daemon.unlock('resource1', 'client1')
Traceback (most recent call last):
Exception: Resource not locked
IMPORTANT security note
Resource identification and client authorization are out of scope for lockd. It should only be used and exposed within a trusted environment.
Cleanup:
>>> import shutil >>> shutil.rmtree(lockdir)
Release files for gocept.lockd 0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gocept.lockd-0.1.tar.gz | 4.0 kB | Details |
Release files / gocept.lockd-0.1.tar.gz
| Download URL | gocept.lockd-0.1.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7d37670134ddede6a09ea4694e8d1dce42b0d0c9b4fc39f370bf84560af3e1d
|
|
BLAKE2b-256 checksum How to use checksums |
948a8f2c13b34bfa1b461b46c6f1bf988eb72ce9100ad98fc84cd02b6cd4f66c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |