A python implementation of the three Reader-Writer problems.
Project description
Reader Writer Lock
A python implementation of 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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size readerwriterlock-1.0.7-py3-none-any.whl (7.2 kB) | File type Wheel | Python version py3 | Upload date | Hashes View |
Filename, size readerwriterlock-1.0.7.tar.gz (14.0 kB) | File type Source | Python version None | Upload date | Hashes View |
Hashes for readerwriterlock-1.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22c2587eee403561250a1b28d871ee5f3b51fdb72b514608881217b20e537834 |
|
MD5 | 3769ec5167ef4248d0f805bef4b9ba0f |
|
BLAKE2-256 | a515bb2aa2f7a67e1212ad2bc5682c8f9feef8f674734bf75f710a9ba8779e55 |