Skip to main content

Read write lock for asyncio.

Project description

https://github.com/aio-libs/aiorwlock/workflows/CI/badge.svg https://codecov.io/gh/aio-libs/aiorwlock/branch/master/graph/badge.svg Chat on Gitter

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.

Example

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())

Fast path

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

TLA+ specification of aiorwlock provided in this repository.

License

aiorwlock is offered under the Apache 2 license.

Changes

1.3.0 (2022-1-18)

  • Dropped Python 3.6 support

  • Python 3.10 is officially supported

  • Drop deprecated loop parameter from RWLock constructor

1.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 in asyncio.sleep() call, make the library forward compatible with Python 3.10

1.0.0 (2020-12-32)

  • Fix a bug with cancelation during acquire #170 (thanks @romasku)

  • Deprecate passing explicit loop argument to RWLock constructor

  • 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

0.6.0 (2018-12-18)

  • 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

0.5.0 (2017-12-03)

  • Fix corner cases and deadlock when we upgrade lock from write to read #39

  • Use loop.create_future instead asyncio.Future if possible

0.4.0 (2015-09-20)

  • Support Python 3.5 and async with statement

  • rename .reader_lock -> .reader, .writer_lock -> .writer. Backward compatibility is preserved.

0.3.0 (2014-02-11)

  • Add .locked property

0.2.0 (2014-02-09)

  • Make .release() non-coroutine

0.1.0 (2014-12-22)

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiorwlock-1.3.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiorwlock-1.3.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file aiorwlock-1.3.0.tar.gz.

File metadata

  • Download URL: aiorwlock-1.3.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aiorwlock-1.3.0.tar.gz
Algorithm Hash digest
SHA256 83f12d87df4b9728a0b8fda1756585ab0d652b107bab59c6084e1b1ad692ab45
MD5 28e4812a1d1d67a2be4370209ece5528
BLAKE2b-256 77fe7027595f5e635ac1f597f7160a420e1f642a474576efb697001efea16bdc

See more details on using hashes here.

File details

Details for the file aiorwlock-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: aiorwlock-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for aiorwlock-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45baf8e4fa9a23e0bb325fbd67da80de1fd7ae1d4f59a6381754c60cec7b289b
MD5 eac13388226ae870421672d2e687dcec
BLAKE2b-256 04c0f0082a7d21fc5cdaeac1433348412e7e8da79197852ed0fec67277a3e768

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page