Cross platform locks for threads and processes.
pip install fasteners
Lock for processes has the same API as the threading.Lock for threads:
import fasteners import threading lock = threading.Lock() # for threads lock = fasteners.InterProcessLock('path/to/lock.file') # for processes with lock: ... # exclusive access # or alternatively lock.acquire() ... # exclusive access lock.release()
Reader Writer lock has a similar API, which is the same for threads or processes:
import fasteners rw_lock = fasteners.ReaderWriterLock() # for threads rw_lock = fasteners.InterProcessReaderWriterLock('path/to/lock.file') # for processes with rw_lock.write_lock(): ... # write access with rw_lock.read_lock(): ... # read access # or alternatively rw_lock.acquire_read_lock() ... # read access rw_lock.release_read_lock() rw_lock.acquire_write_lock() ... # write access rw_lock.release_write_lock()
Python standard library provides a lock for threads (both a reentrant one, and a non-reentrant one, see below). Fasteners extends this, and provides a lock for processes, as well as Reader Writer locks for both threads and processes.
The specifics of the locks are as follows:
fasteners.InterProcessReaderWriterLock also uses fcntl on Unix-like systems and
LockFileEx on Windows. Their
features are as follows:
Fasteners do not provide a simple thread lock, but for the sake of comparison note that the
provides both a reentrant and non-reentrant locks:
fasteners.ReaderWriterLock at the moment is as follows:
To learn more about the various aspects of locks, check the wikipedia pages for locks and readers writer locks as well as the resources listed in the documentation. Here we briefly mention the main notions used above.
- Lock - a mechanism that prevents two or more threads or processes from running the same code at the same time.
- Readers writer lock - a mechanism that prevents two or more threads from having write (or write and read) access, while allowing multiple readers.
- Reentrant lock - a lock that can be acquired (and then released) multiple times, as in:
with lock: with lock: ... # some code
- Mandatory lock (as opposed to advisory lock) - a lock that is enforced by the operating system, rather than by the cooperation between threads or processes
- Upgradable readers writer lock - a readers writer lock that can be upgraded from reader to writer (or downgraded from writer to reader) without losing the lock that is already held, as in:
with rw_lock.read_lock(): ... # read access with rw_lock.write_lock(): ... # write access ... # read access
- Readers writer lock preference - describes the behaviour when multiple threads or processes are waiting for
access. Some of the patterns are:
- Reader preference - If lock is held by readers, then new readers will get immediate access. This can result in writers waiting forever (writer starvation)
- Writer preference - If writer is waiting for a lock, then all the new readers (and writers) will be queued after the writer.
- Phase fair - Lock that alternates between readers and writers.