Skip to content

Instantly share code, notes, and snippets.

@lemiant
Last active May 12, 2022 08:26
Show Gist options
  • Save lemiant/10660092 to your computer and use it in GitHub Desktop.
Save lemiant/10660092 to your computer and use it in GitHub Desktop.
Reader Biased ReadWriteLock in Python
import threading
class ReadWriteLock(object):
""" An implementation of a read-write lock for Python.
Any number of readers can work simultaneously but they
are mutually exclusive with any writers (which can
only have one at a time).
This implementation is reader biased. This can be harmful
under heavy load because it can starve writers.
However under light load it will be quite perfomant since
reads are usually much less resource intensive than writes,
and because it maximizes concurrency.
"""
def __init__(self):
self.__monitor = threading.Lock()
self.__exclude = threading.Lock()
self.readers = 0
def acquire_read(self):
with self.__monitor:
self.readers += 1
if self.readers == 1:
self.__exclude.acquire()
def release_read(self):
with self.__monitor:
self.readers -= 1
if self.readers == 0:
self.__exclude.release()
def acquire_write(self):
self.__exclude.acquire()
def release_write(self):
self.__exclude.release()
@initialed85
Copy link

I spent fscking ages trying to figure this out on my own with two threading.Events, in the end I gave up and I am thankful that you've already done it, lol

@TaeWoo21
Copy link

It helped me a lot to understand the ReadWriteLock concept. Thank you for saving my time. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment