Last active
November 25, 2018 09:17
-
-
Save michalc/6e4b528d04b05e299ca714a9e8fa1dcd to your computer and use it in GitHub Desktop.
Path lock using a global read/write 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 contextlib | |
from fifolock import FifoLock | |
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 PathLock(): | |
def __init__(self): | |
self.lock = FifoLock() | |
@contextlib.asynccontextmanager | |
async def __call__(self, read, write): | |
mode = Write if write else Read | |
async with self.lock(mode): | |
yield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment