Fifolock: Synchronisation primitives for asyncio Python
github.com
github.com
import asyncio
read_lock = asyncio.Lock()
write_lock = asyncio.Lock()
async def read():
async with read_lock:
# shared access
async def write():
async with read_lock, write_lock:
# exclusive access
also the standard library has asyncio.Semaphore, which seems like the exact same thing like the third example?Agreed that similar behaviour to the examples can be achieved in different ways.
The main aim of FifoLock is to provide a (mini) framework for writing more complex locks. The examples are deliberate in that they show how to create more familiar locks, as an attempt at a gentle introduction to the class.
Also, this actually came out of writing https://github.com/michalc/treelock, which uses FifoLock with a lock that has 4 different modes (as opposed to, for example, a read/write lock that has 2), which is the sort of case where I think FifoLock is particularly useful.
Also, I'm not sure if the example you give of the read/write has the same semantics as the meaning of read/write lock in the examples. For example, it looks like the version here, using asyncio.Lock(), wouldn't ever allow multiple readers, while the one based on FifoLock would, as long as there is no writer.
edit: tweaked the description of read/write lock
This is a fairly common issue in concurrent programming.