Reader Writer Lock
A python implementation of a solution for the three Reader-Writer problems.
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
- 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.
- Instantiate an instance of the chosen RWLock class:
from readerwriterlock import rwlock
a = rwlock.RWLockFairD()
- Generate read locks and write locks using the following methods:
a_reader_lock = a.gen_rlock()
a_writer_lock = a.gen_wlock()
- 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
- Project: GitHub
- Éric Larivière ericlariviere@hotmail.com
Metadata
Release files for readerwriterlock 1.0.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| readerwriterlock-1.0.9.tar.gz | 16.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| readerwriterlock-1.0.9-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.6 kB
Release files / readerwriterlock-1.0.9.tar.gz
| Download URL | readerwriterlock-1.0.9.tar.gz |
|---|---|
| Size | 16.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7c4cc003435d7a8ff15b312b0a62a88d9800ba6164af88991f87f8b748f9bea
|
|
BLAKE2b-256 checksum How to use checksums |
a4b96b7c390440ec23bf5fdf33e76d6c3b697a788b983c11cb2739d6541835d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5
|
Release files / readerwriterlock-1.0.9-py3-none-any.whl
| Download URL | readerwriterlock-1.0.9-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8c4b704e60d15991462081a27ef46762fea49b478aa4426644f2146754759ca7
|
|
BLAKE2b-256 checksum How to use checksums |
c25a2f2e7fc026d5e64b5408aa3fbe0296a6407b8481196cae4daacacb3a3ae0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5
|