Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/benjetson/py-rwmutex

Provides a threading read/write mutex lock usable with context managers.
https://github.com/benjetson/py-rwmutex

mutex python python3 threading threading-synchronization

Last synced: 10 days ago
JSON representation

Provides a threading read/write mutex lock usable with context managers.

Awesome Lists containing this project

README

        

# py-rwmutex

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.

## Purpose

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.

## Installation

This package is available from [PyPi](https://pypi.org/project/rwmutex/), which
means it can be easily acquired via [`pip`](https://pypi.org/project/pip/).

Run this command at a shell prompt:

```bash
pip3 install rwmutex
```

## Usage

To get started, import the package like so:

```py
from rwmutex import RWLock
```

Then you can declare a lock object:

```py
lock = RWLock()
```

To use the lock, you can use `with` blocks:

```py
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](example.py).