3 ms·
Fifolock: Synchronisation primitives for asyncio Python
- brian_herman__ 8y agoIs this what you are supposed to use asyncio classes for?
- takeda 8y agoasyncio is a low level cooperative multitasking framework. Locking by all means is an appropriate use of asyncio, if lock can be acquired the program simply executes with interruption, if the lock was previously acquired instead blocking the thread, asyncio makes the thread execute another coroutine.
- greglindahl 8y agoThere are reasonable circumstances when you'd want these. For example, I might have a daemon where a call modifies some state, makes an async outcall, then modifies more state, and then returns an answer. This function might not be re-entrant. If so, you'll want a lock. This is a fairly common issue in concurrent programming.
- Walkman 8y agoIt has a weird API.
- Walkman 8y agoI don't understand the need for this. Can't the Read-Write example simply written like this? 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? https://docs.python.org/3/library/asyncio-sync.html https://docs.python.org/3/library/asyncio-sync.html
- michalc 8y agoHello, author here. 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 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