Skip to main content

Read write lock for asyncio.

Project description

https://travis-ci.com/aio-libs/aiorwlock.svg?branch=master https://coveralls.io/repos/jettify/aiorwlock/badge.png?branch=master 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 with async def

Requires Python 3.5+

import asyncio
import aiorwlock
loop = asyncio.get_event_loop()


async def go():
    rwlock = aiorwlock.RWLock(loop=loop)
    async with rwlock.writer:
        # or same way you can acquire reader lock
        # async with rwlock.reader: pass
        print("inside writer")
        await asyncio.sleep(0.1, loop=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.

License

aiorwlock is offered under the Apache 2 license.

Changes

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-0.6.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

aiorwlock-0.6.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiorwlock-0.6.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for aiorwlock-0.6.0.tar.gz
Algorithm Hash digest
SHA256 cd310f33a7ea3bf6fd76fcf0c1ba6e4d7eef4d5ded59e95859494425fb7e599d
MD5 8eccf28315b4f851f6f7e9332edeec52
BLAKE2b-256 77cf2a2584c4fc1096ae959d7d189f205eb9c872ec58aca2cde16009d2d83b9e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiorwlock-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for aiorwlock-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3de1aac408f55aaca73fb45368b0ea9b5f9c23ca0198c82ad17d35c7ee885152
MD5 04f628e4d54b62e8623ad0e4fc6e5eb9
BLAKE2b-256 773eedb74e6177b49512291edcb5d12a125560f0dd03c1ee8ea15c55c707cdcc

See more details on using hashes here.

Supported by

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