Skip to main content

Reader Writer Lock

A python implementation of a solution for the three Reader-Writer problems.

repo status Active Build Status Coverage Status BugTracker

Python Version Pypi Version

Code size in bytes License

Downloads Downloads Downloads pyReaderWriterLock_repo_star

Not only does it implement the reader-writer problems, it is also compliant with the python lock interface which among others include support for timeout.

For reading about the theory behind the reader-writer problems refer to Wikipedia.

Installation

Install the python package readerwriterlock

python3 -m pip install -U readerwriterlock

Usage

  1. Choose a rwlock class base on your access priority need and feature need which is going to be use by the threads:
Priority +Speed +Downgradable*
Reader priority (aka First readers-writers problem) RWLockRead RWLockReadD
Writer priority (aka Second readers-writers problem) RWLockWrite RWLockWriteD
Fair priority (aka Third readers-writers problem) RWLockFair RWLockFairD

* Downgradable feature allows the locks to be atomically downgraded from being locked in write-mode to locked in read-mode

ⓘ Downgradable classes come with a theoretical ~20% negative effect on performance for acquiring and releasing locks.

  1. Instantiate an instance of the chosen RWLock class:
from readerwriterlock import rwlock
a = rwlock.RWLockFairD()
  1. Generate read locks and write locks using the following methods:
      a_reader_lock = a.gen_rlock()

      a_writer_lock = a.gen_wlock()
  1. Use the generated read/write locks to protect section in your code:

Pythonic usage example

with a.gen_rlock():
  #Read stuff

with a.gen_wlock():
  #Write stuff

Use case (Timeout) example

b = a.gen_wlock()
if b.acquire(blocking=True, timeout=5):
  try:
    #Do stuff
  finally:
    b.release()

Use case (Downgrade) example

b = a.gen_wlock()
if b.acquire():
    try:
        #Read/Write stuff
        b = b.downgrade()
        #Read stuff
    finally:
        b.release()

Live example

Refer to the file test_rwlock.py which has above 90% line coverage of rwlock.py.

The tests can be initiated by doing

make check.test.coverage

Contact

Metadata

Release files for readerwriterlock 1.0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for readerwriterlock 1.0.10
File Size Uploaded
readerwriterlock-1.0.10.tar.gz 16.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for readerwriterlock 1.0.10
File Interpreter ABI Platform
readerwriterlock-1.0.10-py3-none-any.whl Python 3 none any Details

Total release size: 27.0 kB

Release files / readerwriterlock-1.0.10.tar.gz

Download URL readerwriterlock-1.0.10.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d0e803d17569f70700137cd91c4219d2bae718794a1b0da9fd44eb189e58786d
BLAKE2b-256 checksum
How to use checksums
d04aa9eb837332a2a5eb8ab3d500178eadbfbc28d1588f0221a3fa45ece60537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / readerwriterlock-1.0.10-py3-none-any.whl

Download URL readerwriterlock-1.0.10-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
788c28ca2da19681e466609774252eb55a963286c6f7cf8ace13d0be21a69683
BLAKE2b-256 checksum
How to use checksums
629cd496912967d3c8d498e962f61a93bffc9304a0f09fd8e632a90a0b375d37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.10 This release

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page