
Fifolock: Synchronisation primitives for asyncio Python - michalc
https://github.com/michalc/fifolock
======
Walkman
I 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
Hello, 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

------
brian_herman__
Is this what you are supposed to use asyncio classes for?

~~~
greglindahl
There 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
It has a weird API.

