Last active
January 3, 2022 09:46
-
-
Save michalc/ab9bd571cfab09216c0316f2302a76b0 to your computer and use it in GitHub Desktop.
Python asyncio read-write lock, using a generic first-in-first-out lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import collections | |
import contextlib | |
class Read(asyncio.Future): | |
@staticmethod | |
def is_compatible(holds): | |
return not holds[Write] | |
class Write(asyncio.Future): | |
@staticmethod | |
def is_compatible(holds): | |
return not holds[Read] and not holds[Write] | |
class FifoLock(): | |
def __init__(self): | |
self._waiters = collections.deque() | |
self._holds = collections.defaultdict(int) | |
def _maybe_acquire(self): | |
while True: | |
if not self._waiters: | |
break | |
if self._waiters[0].cancelled(): | |
self._waiters.popleft() | |
continue | |
if self._waiters[0].is_compatible(self._holds): | |
waiter = self._waiters.popleft() | |
self._holds[type(waiter)] += 1 | |
waiter.set_result(None) | |
continue | |
break | |
@contextlib.asynccontextmanager | |
async def __call__(self, lock_mode_type): | |
lock_mode = lock_mode_type() | |
self._waiters.append(lock_mode) | |
self._maybe_acquire() | |
try: | |
await lock_mode | |
except asyncio.CancelledError: | |
self._maybe_acquire() | |
raise | |
try: | |
yield | |
finally: | |
self._holds[type(lock_mode)] -= 1 | |
self._maybe_acquire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now available via https://github.com/michalc/fifolock / https://pypi.org/project/fifolock/