A python package that provides useful locks
Project description
Fasteners
Cross-platform locks for threads and processes.
🔩 Install
pip install fasteners
🔩 Usage
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()
🔩 Overview
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. Definitions of terms used in this overview can be found in the glossary.
The specifics of the locks are as follows:
Process locks
The fasteners.InterProcessLock
uses fcntl on Unix-like systems and
msvc _locking on Windows.
As a result, if used cross-platform it guarantees an intersection of their features:
lock | reentrant | mandatory |
---|---|---|
fcntl | ✘ | ✘ |
_locking | ✔ | ✔ |
fasteners.InterProcessLock | ✘ | ✘ |
The fasteners.InterProcessReaderWriterLock
also uses fcntl on Unix-like systems and
LockFileEx on Windows. Their
features are as follows:
lock | reentrant | mandatory | upgradable | preference |
---|---|---|---|---|
fcntl | ✘ | ✘ | ✔ | reader |
LockFileEx | ✔ | ✔ | ✘ | reader |
fasteners.InterProcessReaderWriterLock | ✘ | ✘ | ✘ | reader |
Thread locks
Fasteners does not provide a simple thread lock, but for the sake of comparison note that the threading
module
provides both a reentrant and non-reentrant locks:
lock | reentrant | mandatory |
---|---|---|
threading.Lock | ✘ | ✘ |
threading.RLock | ✔ | ✘ |
The fasteners.ReaderWriterLock
at the moment is as follows:
lock | reentrant | mandatory | upgradable | preference |
---|---|---|---|---|
fasteners.ReaderWriterLock | ✔ | ✘ | ✘ | writer |
If your threads are created by some other means than the standard library threading
module (for example eventlet
), you may need to provide the corresponding thread
identification and synchronisation functions to the ReaderWriterLock
.
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
File details
Details for the file fasteners-0.19.tar.gz
.
File metadata
- Download URL: fasteners-0.19.tar.gz
- Upload date:
- Size: 24.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4f37c3ac52d8a445af3a66bce57b33b5e90b97c696b7b984f530cf8f0ded09c |
|
MD5 | e957e11c2e00629e44166e1d2881798e |
|
BLAKE2b-256 | 5fd4e834d929be54bfadb1f3e3b931c38e956aaa3b235a46a3c764c26c774902 |
File details
Details for the file fasteners-0.19-py3-none-any.whl
.
File metadata
- Download URL: fasteners-0.19-py3-none-any.whl
- Upload date:
- Size: 18.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 758819cb5d94cdedf4e836988b74de396ceacb8e2794d21f82d131fd9ee77237 |
|
MD5 | 0e45841c638ae0fb3c228844904007f2 |
|
BLAKE2b-256 | 61bffd60001b3abc5222d8eaa4a204cd8c0ae78e75adc688f33ce4bf25b7fafa |