{"id":2747,"library":"readerwriterlock","title":"Reader-Writer Lock","description":"The `readerwriterlock` library provides a Python 3 implementation of the three classic Reader-Writer problems. It offers different lock types (Reader priority, Writer priority, Fair priority) and is compliant with the standard Python lock interface, including support for timeouts. The current version is 1.0.9, and releases are infrequent, indicating a stable and mature library.","status":"active","version":"1.0.9","language":"en","source_language":"en","source_url":"https://github.com/elarivie/pyReaderWriterLock","tags":["concurrency","threading","lock","reader-writer lock","synchronization"],"install":[{"cmd":"pip install readerwriterlock","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Requires Python 3.6 or higher for execution.","package":"Python","optional":false}],"imports":[{"note":"The lock classes are nested within the 'rwlock' submodule, not directly under 'readerwriterlock'.","wrong":"from readerwriterlock import RWLockFair","symbol":"RWLockRead, RWLockWrite, RWLockFair","correct":"from readerwriterlock import rwlock\nlock = rwlock.RWLockFair()"},{"note":"These are the downgradable versions of the locks.","symbol":"RWLockReadD, RWLockWriteD, RWLockFairD","correct":"from readerwriterlock import rwlock\nlock = rwlock.RWLockFairD()"}],"quickstart":{"code":"from readerwriterlock import rwlock\nimport threading\nimport time\n\n# Choose a fair, downgradable lock for a balanced approach\nlock = rwlock.RWLockFairD()\n\nshared_resource = []\n\ndef reader(reader_id):\n    with lock.gen_rlock():\n        # Multiple readers can enter this block concurrently\n        print(f\"Reader {reader_id}: Reading {shared_resource}\")\n        time.sleep(0.1) # Simulate read operation\n\ndef writer(writer_id):\n    with lock.gen_wlock():\n        # Only one writer can enter this block\n        old_val = list(shared_resource)\n        shared_resource.append(writer_id)\n        print(f\"Writer {writer_id}: Wrote {writer_id}, was {old_val}, now {shared_resource}\")\n        time.sleep(0.2) # Simulate write operation\n\n\nthreads = []\nfor i in range(3):\n    threads.append(threading.Thread(target=reader, args=(f'R{i}',)))\n    threads.append(threading.Thread(target=writer, args=(f'W{i}',)))\n\nfor t in threads:\n    t.start()\nfor t in threads:\n    t.join()\n\nprint(f\"Final resource state: {shared_resource}\")","lang":"python","description":"This example demonstrates how to use `RWLockFairD` to protect a shared resource. Multiple reader threads can access the resource concurrently, while writer threads gain exclusive access. The `gen_rlock()` and `gen_wlock()` methods create context managers for acquiring and releasing read and write locks, respectively."},"warnings":[{"fix":"Upgrade `readerwriterlock` to version 1.0.9 or newer. Alternatively, upgrade Python to a version beyond 3.9.7 (e.g., 3.9.8+ or 3.10+).","message":"Versions prior to 1.0.9, when used with Python 3.9.7, could encounter a `TypeError: Protocols cannot be instantiated` due to an incompatibility with `typing.Protocol`.","severity":"gotcha","affected_versions":"<1.0.9"},{"fix":"For performance-critical sections where atomic downgrade from write-lock to read-lock is not required, use the non-downgradable lock classes (`RWLockRead`, `RWLockWrite`, `RWLockFair`).","message":"The downgradable lock classes (`RWLockReadD`, `RWLockWriteD`, `RWLockFairD`) introduce a theoretical ~20% performance overhead for acquiring and releasing locks compared to their non-downgradable counterparts.","severity":"gotcha","affected_versions":"All versions with downgradable locks"},{"fix":"For inter-process or distributed reader-writer locks, consider alternative libraries like `fasteners.InterProcessReaderWriterLock` or `redisrwlock`, or implement custom solutions using `multiprocessing.Lock` or `ctypes` and `mmap` for POSIX shared memory locks.","message":"This library implements reader-writer locks for *threads* within a single process. It does not support inter-process communication or distributed locking.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review the Python standard library documentation for `threading.RWLock` (when available in Python 3.13+) to evaluate if it meets your needs.","message":"Python 3.13+ is planned to include a built-in `threading.RWLock`. Users on newer Python versions may consider using the standard library implementation over this third-party library.","severity":"gotcha","affected_versions":"N/A (future Python versions)"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}