Skip to main content

A python package that provides useful locks

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fasteners-0.20.tar.gz (25.1 kB view details)

Uploaded Source

Built Distribution

fasteners-0.20-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file fasteners-0.20.tar.gz.

File metadata

  • Download URL: fasteners-0.20.tar.gz
  • Upload date:
  • Size: 25.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for fasteners-0.20.tar.gz
Algorithm Hash digest
SHA256 55dce8792a41b56f727ba6e123fcaee77fd87e638a6863cec00007bfea84c8d8
MD5 dd412feb9e6a7e5d167810016b85ac2b
BLAKE2b-256 2d187881a99ba5244bfc82f06017316ffe93217dbbbcfa52b887caa1d4f2a6d3

See more details on using hashes here.

File details

Details for the file fasteners-0.20-py3-none-any.whl.

File metadata

  • Download URL: fasteners-0.20-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for fasteners-0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 9422c40d1e350e4259f509fb2e608d6bc43c0136f79a00db1b49046029d0b3b7
MD5 f086db6e7d8f0a9f64faedca45b1951c
BLAKE2b-256 51ace5d886f892666d2d1e5cb8c1a41146e1d79ae8896477b1153a21711d3b44

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page