Skip to main content

A python package that provides useful locks.

Project description

Fasteners

Documentation status Downloads 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_locked():
    ... # write access

with rw_lock.read_locked():
    ... # 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.

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 do 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 reader

🔩 Glossary

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_locked():
    ... # read access
    with rw_lock.write_locked():
        ... # 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.

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.16.3.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

fasteners-0.16.3-py2.py3-none-any.whl (28.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: fasteners-0.16.3.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for fasteners-0.16.3.tar.gz
Algorithm Hash digest
SHA256 b1ab4e5adfbc28681ce44b3024421c4f567e705cc3963c732bf1cba3348307de
MD5 243188fe770ad60e9da722bef9dc7a78
BLAKE2b-256 28e42888d41cdbd405828ccdb9a8536c5919939c2f4c6ab9b2ba63e9bd2570d5

See more details on using hashes here.

File details

Details for the file fasteners-0.16.3-py2.py3-none-any.whl.

File metadata

  • Download URL: fasteners-0.16.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for fasteners-0.16.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8408e52656455977053871990bd25824d85803b9417aa348f10ba29ef0c751f7
MD5 cda05f453dec760fe242857c89c7b07b
BLAKE2b-256 31916630ebd169ca170634ca8a10dfcc5f5c11b0621672d4c2c9e40381c6d81a

See more details on using hashes here.

Supported by

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