Package: aiorwlock
Read write lock forasyncio. ARWLockmaintains 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 isreadcompared to beingmodified. 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 thispatch.Exampleimportasyncioimportaiorwlockasyncdefgo():rwlock=aiorwlock.RWLock()# acquire reader lock, multiple coroutines allowed to hold the lockasyncwithrwlock.reader_lock:print('inside reader lock')awaitasyncio.sleep(0.1)# acquire writer lock, only one coroutine can hold the lockasyncwithrwlock.writer_lock:print('inside writer lock')awaitasyncio.sleep(0.1)asyncio.run(go())Fast pathBy defaultRWLockswitches 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 futstatements).The default behavior can be switched off byfastargument:RWLock(fast=True).Long story short:  lock is safe by  default, but if you  sure you have
context switches (await,async with,async fororyield fromstatements) inside  locked code  you may want  to usefast=Truefor
minor speedup.TLA+ SpecificationTLA+ specification ofaiorwlockprovided in this repository.Licenseaiorwlockis offered under the Apache 2 license.Changes1.4.0 (2024-01-20)Lazily evaluate current loop to allow instantiating lock outside of async functions.Support Python 3.11 and 3.12.Drop Python 3.7 support.1.3.0 (2022-01-18)Dropped Python 3.6 supportPython 3.10 is officially supportedDrop deprecatedloopparameter fromRWLockconstructor1.2.0 (2021-11-09)Fix a bug that makes concurrent writes possible under some (rare) conjunctions (#235)1.1.0 (2021-09-27)Remove explicit loop usage inasyncio.sleep()call, make the library forward
compatible with Python 3.101.0.0 (2020-12-32)Fix a bug with cancelation during acquire #170 (thanks @romasku)Deprecate passing explicitloopargument toRWLockconstructorDeprecate creation ofRWLockinstance outside of async function contextMinimal supported version is Python 3.6The library works with Python 3.8 and Python 3.9 seamlessly0.6.0 (2018-12-18)Wake up all readers after writer releases lock #60 (thanks @ranyixu)Fixed Python 3.7 compatibilityRemoved oldyield fromsyntaxMinimal supported version is Python 3.5.3Removed support for none async context managers0.5.0 (2017-12-03)Fix corner cases and deadlock when we upgrade lock from write to
read #39Use loop.create_future instead asyncio.Future if possible0.4.0 (2015-09-20)Support Python 3.5 andasync withstatementrename.reader_lock->.reader,.writer_lock->.writer. Backward compatibility is preserved.0.3.0 (2014-02-11)Add.lockedproperty0.2.0 (2014-02-09)Make.release()non-coroutine0.1.0 (2014-12-22)Initial release