This package provides a simple read/write mutex lock for threads, based upon
the threading
package. It supports Python’s context manager interface, so
it may be used within with
statements.
This read/write lock can improve performance by allowing multiple threads to reaad from a shared resource at once. For safety, only one thread is granted a write lock at a time, and only when no threads have a read lock.
This package is available from PyPi, which
means it can be easily acquired via pip
.
Run this command at a shell prompt:
pip3 install rwmutex
To get started, import the package like so:
from rwmutex import RWLock
Then you can declare a lock object:
lock = RWLock()
To use the lock, you can use with
blocks:
with lock.write
# some operation that writes
pass
with lock.read:
# some operation that just needs to read
pass
For a working example, see example.py.