A Python locking library not depending on inter-process locking primitives in the OS
Project description
openlock
A locking library not depending on inter-process locking primitives in the OS.
API
FileLock(lock_file="openlock.lock", timeout=None)
. Constructor. The optionaltimeout
argument is the default for the corresponding argument ofacquire()
(see below). AFileLock
object supports the context manager protocol.FileLock.acquire(timeout=None)
. Attempts to acquire the lock. The optionaltimeout
argument specifies the maximum waiting time in seconds before aTimeout
exception is raised.FileLock.release()
. Releases the lock. May raise anInvalidRelease
exception.FileLock.locked()
. Indicates if the lock is held by a process.FileLock.getpid()
. The PID of the process that holds the lock, if any. Otherwise returnsNone
.FileLock.lock_file
. The name of the lock file.FileLock.timeout
. The value of the timeout parameter.openlock.set_defaults(**kw)
. Sets default values for the internal parameters. Currentlytries
,retry_period
,race_delay
andslow_system_exception
with values of 2, 0.3s, 0.2s and False respectively.openlock.get_defaults()
. Returns a dictionary with the default values for the internal parameters.
How does it work
A valid lock file has two lines of text containing respectively:
pid
: the PID of the process holding the lock;name
: the content ofargv[0]
of the process holding the lock.
A lock file is considered stale if the pair (pid, name)
does not belong to a Python process in the process table.
A process that seeks to acquire a lock first looks for an existing valid lock file. If it exists then this means that the lock has already been acquired and the process will periodically retry to acquire it - subject to the timeout
parameter. If there is no lock file, or if it is stale or unparsable, then the process atomically creates a new lock file with its own data. It sleeps 0.2 seconds (configurable) and then checks if the lock file has been overwritten by a different process. If not then it has acquired the lock.
Issues
-
The algorithm fails if a process needs more than 0.2 seconds to create a new lock file after detecting the absence of a valid one. The library will issue a warning if it thinks the system is too slow for the algorithm to work correctly and it will recommend to increase the value of the
race_delay
parameter. -
Although it is very unlikely, it may be that the data
(pid, name)
matches a different Python process since PIDs are only unique over the lifetime of a process. In that case the algorithm fails to recognize the lock file as stale.
History
This is a refactored version of the locking algorithm used by the worker for the Fishtest web application https://tests.stockfishchess.org/tests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for openlock-1.0.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fcc173518a9514f959ce8ac860f193a9fea6abff03a7749a5f63432cd0a81258 |
|
MD5 | 00f856b1fd5c9425302fcd3d2236c8b1 |
|
BLAKE2b-256 | e4544e38727e66cc0274377a5b90630c0a9153e88593d42d4876b3b1cda156ca |