fasteners

A python package that provides useful locks.

Latest version: 0.20 registry icon
Maintenance score
13
Safety score
100
Popularity score
19
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.20 0 0 0 0 0
0.19 0 0 0 0 0
0.18 0 0 0 0 0
0.17.3 0 0 0 0 0
0.17.2 0 0 0 0 0
0.17.1 0 0 0 0 0
0.16.3 0 0 0 0 0
0.16.2 0 0 0 0 0
0.16.1 0 0 0 0 0
0.16 0 0 0 0 0
0.15 0 0 0 0 0
0.14.1 0 0 0 0 0
0.14.0 0 0 0 0 0
0.13.0 0 0 0 0 0
0.12.0 0 0 0 0 0
0.11.0 0 0 0 0 0
0.10.0 0 0 0 0 0
0.9.0 0 0 0 0 0
0.8.0 0 0 0 0 0
0.7.0 0 0 0 0 0
0.6.0 0 0 0 0 0
0.5.0 0 0 0 0 0
0.4.0 0 0 0 0 0
0.3.0 0 0 0 0 0

Stability
Latest release:

0.20 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-2.0   -   Apache License 2.0

Not a wildcard

Not proprietary

OSI Compliant



Fasteners

Documentation status Latest version

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.