{"id":2375,"library":"aiorwlock","title":"aiorwlock: Read Write Lock for Asyncio","description":"aiorwlock provides a read-write lock, a synchronization primitive for `asyncio` applications. It allows multiple reader tasks to hold a lock concurrently, while a writer task obtains an exclusive lock, blocking all readers and other writers. This pattern is ideal for resources that are frequently read but infrequently written. The library is currently at version 1.5.1 and maintains an active release cadence with several minor and patch updates per year.","status":"active","version":"1.5.1","language":"en","source_language":"en","source_url":"https://github.com/aio-libs/aiorwlock","tags":["asyncio","lock","read-write lock","synchronization","concurrency"],"install":[{"cmd":"pip install aiorwlock","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required runtime environment.","package":"python","optional":false}],"imports":[{"symbol":"RWLock","correct":"from aiorwlock import RWLock"}],"quickstart":{"code":"import asyncio\nfrom aiorwlock import RWLock\n\nasync def reader_task(rwlock: RWLock, reader_id: int):\n    print(f\"Reader {reader_id}: Attempting to acquire read lock\")\n    async with rwlock.reader_lock:\n        print(f\"Reader {reader_id}: Acquired read lock\")\n        await asyncio.sleep(0.05) # Simulate reading\n        print(f\"Reader {reader_id}: Released read lock\")\n\nasync def writer_task(rwlock: RWLock, writer_id: int):\n    print(f\"Writer {writer_id}: Attempting to acquire write lock\")\n    async with rwlock.writer_lock:\n        print(f\"Writer {writer_id}: Acquired write lock\")\n        await asyncio.sleep(0.1) # Simulate writing\n        print(f\"Writer {writer_id}: Released write lock\")\n\nasync def main():\n    rwlock = RWLock()\n    tasks = []\n    # Start some readers\n    for i in range(3):\n        tasks.append(asyncio.create_task(reader_task(rwlock, i)))\n    # Start a writer in between\n    tasks.append(asyncio.create_task(writer_task(rwlock, 100)))\n    # Start more readers\n    for i in range(3, 6):\n        tasks.append(asyncio.create_task(reader_task(rwlock, i)))\n\n    await asyncio.gather(*tasks)\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This example demonstrates basic usage of `RWLock` with multiple concurrent readers and a single writer. Readers can acquire the `reader_lock` simultaneously, but a writer acquiring the `writer_lock` will block all other readers and writers."},"warnings":[{"fix":"Upgrade Python to 3.8 or newer. The current minimum supported version is Python 3.9.","message":"Python 3.7 support was dropped in v1.4.0. Users on Python 3.7 or older must upgrade their Python version to use `aiorwlock` v1.4.0 or newer.","severity":"breaking","affected_versions":">=1.4.0"},{"fix":"Remove the `loop` argument from `RWLock()` constructor calls. `aiorwlock` now lazily evaluates the current event loop.","message":"The explicit `loop` parameter for the `RWLock` constructor was deprecated in v1.0.0 and removed in v1.3.0. Passing a `loop` argument will now raise an error.","severity":"breaking","affected_versions":">=1.3.0"},{"fix":"Ensure `RWLock()` instances are created within an `async` function or a context where `asyncio.get_running_loop()` can correctly resolve the event loop.","message":"Creation of `RWLock` instances outside of an `async` function context was deprecated in v1.0.0. While it might still work in some setups due to lazy loop evaluation, it's considered bad practice.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Upgrade to `aiorwlock` v1.5.1 or newer to benefit from critical stability fixes.","message":"Fixed a cross-event-loop race condition in lock acquisition and a deadlock that could occur when tasks are cancelled.","severity":"gotcha","affected_versions":"<1.5.1"},{"fix":"Upgrade to `aiorwlock` v1.2.0 or newer to ensure correct exclusive write lock behavior.","message":"Fixed a bug that allowed concurrent writes under rare conditions.","severity":"gotcha","affected_versions":"<1.2.0"},{"fix":"Always ensure that the same `async` task (coroutine) that acquires a `reader_lock` or `writer_lock` is also responsible for releasing it (e.g., by using `async with`).","message":"A `RuntimeError` will be raised if a lock is acquired by one task but released by another. Synchronization primitives in `asyncio` are typically task-bound.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}