Registry / serialization / readerwriterlock

readerwriterlock

JSON →
library1.0.9pypypi✓ verified 26d ago

The `readerwriterlock` library provides a Python 3 implementation of the three classic Reader-Writer problems. It offers different lock types (Reader priority, Writer priority, Fair priority) and is compliant with the standard Python lock interface, including support for timeouts. The current version is 1.0.9, and releases are infrequent, indicating a stable and mature library.

pip install readerwriterlock
INSTALL
IMPORT
SIG · READERWRITERLOCK
R
readerwriterlock
serializationpythonv1.0.9
Install
1.6s avg
Import
45ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.046s · 18.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.044s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RWLockRead, RWLockWrite, RWLockFair
from readerwriterlock import rwlock lock = rwlock.RWLockFair()
from readerwriterlock import RWLockFair
The lock classes are nested within the 'rwlock' submodule, not directly under 'readerwriterlock'.
RWLockReadD, RWLockWriteD, RWLockFairD
from readerwriterlock import rwlock lock = rwlock.RWLockFairD()
These are the downgradable versions of the locks.

This example demonstrates how to use `RWLockFairD` to protect a shared resource. Multiple reader threads can access the resource concurrently, while writer threads gain exclusive access. The `gen_rlock()` and `gen_wlock()` methods create context managers for acquiring and releasing read and write locks, respectively.

from readerwriterlock import rwlock import threading import time # Choose a fair, downgradable lock for a balanced approach lock = rwlock.RWLockFairD() shared_resource = [] def reader(reader_id): with lock.gen_rlock(): # Multiple readers can enter this block concurrently print(f"Reader {reader_id}: Reading {shared_resource}") time.sleep(0.1) # Simulate read operation def writer(writer_id): with lock.gen_wlock(): # Only one writer can enter this block old_val = list(shared_resource) shared_resource.append(writer_id) print(f"Writer {writer_id}: Wrote {writer_id}, was {old_val}, now {shared_resource}") time.sleep(0.2) # Simulate write operation threads = [] for i in range(3): threads.append(threading.Thread(target=reader, args=(f'R{i}',))) threads.append(threading.Thread(target=writer, args=(f'W{i}',))) for t in threads: t.start() for t in threads: t.join() print(f"Final resource state: {shared_resource}")
Debug
Known issues
gotchaVersions prior to 1.0.9, when used with Python 3.9.7, could encounter a `TypeError: Protocols cannot be instantiated` due to an incompatibility with `typing.Protocol`.
fix
Upgrade `readerwriterlock` to version 1.0.9 or newer. Alternatively, upgrade Python to a version beyond 3.9.7 (e.g., 3.9.8+ or 3.10+).
affects: <1.0.9
gotchaThe downgradable lock classes (`RWLockReadD`, `RWLockWriteD`, `RWLockFairD`) introduce a theoretical ~20% performance overhead for acquiring and releasing locks compared to their non-downgradable counterparts.
fix
For performance-critical sections where atomic downgrade from write-lock to read-lock is not required, use the non-downgradable lock classes (`RWLockRead`, `RWLockWrite`, `RWLockFair`).
affects: All versions with downgradable locks
gotchaThis library implements reader-writer locks for *threads* within a single process. It does not support inter-process communication or distributed locking.
fix
For inter-process or distributed reader-writer locks, consider alternative libraries like `fasteners.InterProcessReaderWriterLock` or `redisrwlock`, or implement custom solutions using `multiprocessing.Lock` or `ctypes` and `mmap` for POSIX shared memory locks.
affects: All versions
gotchaPython 3.13+ is planned to include a built-in `threading.RWLock`. Users on newer Python versions may consider using the standard library implementation over this third-party library.
fix
Review the Python standard library documentation for `threading.RWLock` (when available in Python 3.13+) to evaluate if it meets your needs.
affects: N/A (future Python versions)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'readerwriterlock'
The 'readerwriterlock' package is not installed in the Python environment or is not accessible from the current interpreter.
fix
pip install readerwriterlock
ImportError: cannot import name 'RWLockFair' from 'readerwriterlock'
The RWLock classes (e.g., RWLockFair, RWLockRead, RWLockWrite) are located within the 'rwlock' submodule, not directly under the top-level 'readerwriterlock' package.
fix
from readerwriterlock.rwlock import RWLockFair
RuntimeError: cannot release un-acquired lock
An attempt was made to call a release method (e.g., `writer_release()`, `reader_release()`) on a lock that was not previously acquired by the calling thread.
fix
Ensure every `release()` call is preceded by a corresponding `acquire()` call within the same thread, typically managed using `with` statements or `try...finally` blocks.
AttributeError: 'RWLockFair' object has no attribute 'acquire_read'
The method name `acquire_read` (or similar variations like `acquire_write`) is incorrect; the library uses `reader_acquire` and `writer_acquire`.
fix
Use `lock.reader_acquire()` to acquire a read lock or `lock.writer_acquire()` to acquire a write lock.
Upgrade
Version history
1.0.9latest on PyPI · released Sep 6, 2021
Audit
Dependencies
PythonrequiredRequires Python 3.6 or higher for execution.
Agent activity
7 hits · last 30 days
node
6
Resources
readerwriterlock — pip install readerwriterlock · libregistry