Fifolock

0.0.20 · active · verified Thu Apr 16

Fifolock is a Python library providing flexible, low-level synchronization primitives for `asyncio` applications. It implements first-in-first-out (FIFO) ordered locks, ensuring requests are granted strictly in the order they are made. The current version is 0.0.20. The project appears to have an infrequent release cadence, with the last significant activity several years ago.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic mutex using `FifoLock`. By defining a `Mutex` class that inherits from `asyncio.Future` and implementing `is_compatible`, you create a lock 'mode'. The `async with lock(Mutex):` statement ensures that only one coroutine can hold the mutex at a time, with acquisition in FIFO order.

import asyncio
from fifolock import FifoLock

class Mutex(asyncio.Future):
    @staticmethod
    def is_compatible(holds):
        return not holds[Mutex]

lock = FifoLock()

async def access_resource(task_id):
    print(f"Task {task_id}: Requesting lock")
    async with lock(Mutex):
        print(f"Task {task_id}: Lock acquired, accessing resource...")
        await asyncio.sleep(0.1) # Simulate work
        print(f"Task {task_id}: Resource released.")

async def main():
    tasks = [access_resource(i) for i in range(5)]
    await asyncio.gather(*tasks)

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →