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.
Metadata
Release files for fasteners 0.20
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fasteners-0.20.tar.gz | 25.1 kB | Without provenance | Details for fasteners-0.20.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fasteners-0.20-py3-none-any.whl | Python 3 | none | any | Details for fasteners-0.20-py3-none-any.whl |
Total release size: 43.8 kB
Release files / fasteners-0.20.tar.gz
| Download URL | fasteners-0.20.tar.gz |
|---|---|
| Size | 25.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
55dce8792a41b56f727ba6e123fcaee77fd87e638a6863cec00007bfea84c8d8
|
|
BLAKE2b-256 checksum How to use checksums |
2d187881a99ba5244bfc82f06017316ffe93217dbbbcfa52b887caa1d4f2a6d3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / fasteners-0.20-py3-none-any.whl
| Download URL | fasteners-0.20-py3-none-any.whl |
|---|---|
| Size | 18.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9422c40d1e350e4259f509fb2e608d6bc43c0136f79a00db1b49046029d0b3b7
|
|
BLAKE2b-256 checksum How to use checksums |
51ace5d886f892666d2d1e5cb8c1a41146e1d79ae8896477b1153a21711d3b44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for fasteners 0.20
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.