All Projects → aio-libs → aiorwlock

aio-libs / aiorwlock

Licence: Apache-2.0 license
Read/Write Lock - synchronization primitive for asyncio

Programming Languages

python
139335 projects - #7 most used programming language
TLA
29 projects
Makefile
30231 projects

Projects that are alternatives of or similar to aiorwlock

Java Concurrency Examples
Java Concurrency/Multithreading Tutorial with Examples for Dummies
Stars: ✭ 173 (+92.22%)
Mutual labels:  concurrency, lock
futex
File-based Ruby Mutex
Stars: ✭ 14 (-84.44%)
Mutual labels:  concurrency, lock
Mayhem
The Python asyncio tutorial I wish existed earlier
Stars: ✭ 184 (+104.44%)
Mutual labels:  concurrency, asyncio
Vibe Core
Repository for the next generation of vibe.d's core package.
Stars: ✭ 56 (-37.78%)
Mutual labels:  concurrency, asyncio
optimistic lock coupling rs
🍋: A General Lock following paper "Optimistic Lock Coupling: A Scalable and Efficient General-Purpose Synchronization Method"
Stars: ✭ 21 (-76.67%)
Mutual labels:  concurrency, lock
Aiochan
CSP-style concurrency for Python
Stars: ✭ 116 (+28.89%)
Mutual labels:  concurrency, asyncio
Pyee
A port of Node.js's EventEmitter to python
Stars: ✭ 236 (+162.22%)
Mutual labels:  concurrency, asyncio
Python3 Concurrency Pics 02
爬取 www.mzitu.com 全站图片,截至目前共5162个图集,16.5万多张美女图片,使用 asyncio 和 aiohttp 实现的异步版本只需要不到2小时就能爬取完成。按日期创建图集目录,保存更合理。控制台只显示下载的进度条,详细信息保存在日志文件中。支持异常处理,不会终止爬虫程序。失败的请求,下次再执行爬虫程序时会自动下载
Stars: ✭ 275 (+205.56%)
Mutual labels:  concurrency, asyncio
python3-concurrency
Python3爬虫系列的理论验证,首先研究I/O模型,分别用Python实现了blocking I/O、nonblocking I/O、I/O multiplexing各模型下的TCP服务端和客户端。然后,研究同步I/O操作(依序下载、多进程并发、多线程并发)和异步I/O(asyncio)之间的效率差别
Stars: ✭ 49 (-45.56%)
Mutual labels:  concurrency, asyncio
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+15.56%)
Mutual labels:  concurrency, lock
Asyncio
asyncio historical repository
Stars: ✭ 952 (+957.78%)
Mutual labels:  concurrency, asyncio
think-async
🌿 Exploring cooperative concurrency primitives in Python
Stars: ✭ 178 (+97.78%)
Mutual labels:  concurrency, asyncio
Mt
tlock, RWMUTEX, Collab, USM, RSem and other C++ templates for Windows to provide read/write mutex locks, various multithreading tools, collaboration, differential updates and more
Stars: ✭ 18 (-80%)
Mutual labels:  concurrency, lock
Ignareo Isml Auto Voter
Ignareo the Carillon, a web spider template of ultimate concurrency built for leprechauns. Carillons as the best web spiders; Long live the golden years of leprechauns!
Stars: ✭ 154 (+71.11%)
Mutual labels:  concurrency, asyncio
Arq
Fast job queuing and RPC in python with asyncio and redis.
Stars: ✭ 695 (+672.22%)
Mutual labels:  concurrency, asyncio
Concurrent
Functional Concurrency Primitives
Stars: ✭ 206 (+128.89%)
Mutual labels:  concurrency, lock
Aioredlock
🔒 The asyncio implemetation of Redis distributed locks
Stars: ✭ 171 (+90%)
Mutual labels:  lock, asyncio
AtomicKit
Concurrency made simple in Swift.
Stars: ✭ 88 (-2.22%)
Mutual labels:  concurrency, lock
django-concurrency-talk
🎭 Database Integrity in Django: Safely Handling Critical Data in Distributed Systems
Stars: ✭ 49 (-45.56%)
Mutual labels:  concurrency, lock
haxe-concurrent
A haxelib for basic platform-agnostic concurrency support
Stars: ✭ 69 (-23.33%)
Mutual labels:  concurrency, lock

aiorwlock

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].