Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

readerwriterlock

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

readerwriterlock

A python implementation of the three Reader-Writer problems.

  • 1.0.9
  • Source
  • PyPI
  • Socket score

Maintainers
1

Reader Writer Lock

A python implementation of a solution for the three Reader-Writer problems.

repo status Active Build Status Coverage Status BugTracker

Python Version Pypi Version

Code size in bytes License

Downloads Downloads Downloads pyReaderWriterLock_repo_star

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

  1. 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)RWLockReadRWLockReadD
Writer priority (aka Second readers-writers problem)RWLockWriteRWLockWriteD
Fair priority (aka Third readers-writers problem)RWLockFairRWLockFairD

* 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.

  1. Instantiate an instance of the chosen RWLock class:
from readerwriterlock import rwlock
a = rwlock.RWLockFairD()
  1. Generate read locks and write locks using the following methods:
      a_reader_lock = a.gen_rlock()

      a_writer_lock = a.gen_wlock()
  1. 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

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc