Read write lock for asyncio.
Read write lock for asyncio . A RWLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader tasks, so long as there are no writers. The write lock is exclusive.
Whether or not a read-write lock will improve performance over the use of a mutual exclusion lock depends on the frequency that the data is read compared to being modified. For example, a collection that is initially populated with data and thereafter infrequently modified, while being frequently searched is an ideal candidate for the use of a read-write lock. However, if updates become frequent then the data spends most of its time being exclusively locked and there is little, if any increase in concurrency.
Implementation is almost direct port from this patch.
Requires Python 3.5.3+
import asyncio import aiorwlock async def go(): rwlock = aiorwlock.RWLock() # acquire reader lock, multiple coroutines allowed to hold the lock async with rwlock.reader_lock: print('inside reader lock') await asyncio.sleep(0.1) # acquire writer lock, only one coroutine can hold the lock async with rwlock.writer_lock: print('inside writer lock') await asyncio.sleep(0.1) loop = asyncio.get_event_loop() loop.run_until_complete(go())
By default RWLock switches context on lock acquiring. That allows to other waiting tasks get the lock even if task that holds the lock doesn’t contain context switches (await fut statements).
The default behavior can be switched off by fast argument: RWLock(fast=True).
Long story short: lock is safe by default, but if you sure you have context switches (await, async with, async for or yield from statements) inside locked code you may want to use fast=True for minor speedup.
TLA+ specification of aiorwlock provided in this repository.
aiorwlock is offered under the Apache 2 license.
- Fix a bug with cancelation during acquire #170 (thanks @romasku)
- Deprecate passing explicit loop argument to RWLock constuctor
- Deprecate creation of RWLock instance outside of async function context
- Minimal supported version is Python 3.6
- The library works with Python 3.8 and Python 3.9 seamlessly
- Wake up all readers after writer releases lock #60 (thanks @ranyixu)
- Fixed Python 3.7 compatibility
- Removed old yield from syntax
- Minimal supported version is Python 3.5.3
- Removed support for none async context managers
- Fix corner cases and deadlock when we upgrade lock from write to read #39
- Use loop.create_future instead asyncio.Future if possible
- Support Python 3.5 and async with statement
- rename .reader_lock -> .reader, .writer_lock -> .writer. Backward compatibility is preserved.
- Add .locked property
- Make .release() non-coroutine
- Initial release
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size aiorwlock-1.0.0-py3-none-any.whl (9.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size aiorwlock-1.0.0.tar.gz (13.4 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for aiorwlock-1.0.0-py3-none-any.whl