Git Product home page Git Product logo

aiorwlock's Introduction

aiorwlock

https://travis-ci.org/aio-libs/aiorwlock.svg?branch=master https://coveralls.io/repos/jettify/aiorwlock/badge.png?branch=master

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

Old-school way

Requires Python 3.3+

import asyncio
import aiorwlock
loop = asyncio.get_event_loop()


@asyncio.coroutine
def go():
    rwlock = aiorwlock.RWLock(loop=loop)
    with (yield from rwlock.writer):
        # or same way you can acquire reader lock
        # with (yield from rwlock.reader): pass
        print("inside writer")
        yield from 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.

aiorwlock's People

Contributors

asvetlov avatar jettify avatar nickolai-dr avatar pyup-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.