/aiofilelock

Primary LanguagePythonOtherNOASSERTION

aiofilelock

PyPI PyPI PyPI Say Thanks!

Mutable and immutable file lock for asyncio

There are two possible locks exclusive (mutable file) and shared (immutable file).

from aiofilelock import AIOMutableFileLock
import asyncio

async def main():
    with open('your-file', 'r+') as f:
        async with AIOMutableFileLock(f):
            f.write('VERY IMPORTANT DATA')
    with open('your-file', 'r+') as f:
        async with AIOImmutableFileLock(f):
            print(f.read())

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

There is also timeout, after which the lock will raise a BlockingIOError and granularity, the parameter of roughness of lock trials, both are in float seconds. Negative timeout acts like non-blocking lock, None — infinite waiting time.

from aiofilelock import AIOMutableFileLock
import asyncio

async def main():
    try:
        with open('your-file', 'r+') as f:
            # raise exception after 3 seconds, try flock each 0.5 seconds.
            async with AIOMutableFileLock(f, timeout=3, granularity=0.5):
                f.write('VERY IMPORTANT DATA')
    except BlockingIOError:
        print("couldn't acquire lock in time")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())