{"id":2948,"library":"fastrlock","title":"Fast Re-entrant Optimistic Lock","description":"Fastrlock is a C-level implementation of a re-entrant, optimistic lock for CPython, serving as a drop-in replacement for `threading.RLock`. Implemented in Cython, it offers both a Python and a C-API, designed to optimize for non-congested locking scenarios. It is actively maintained with periodic updates to support newer Python versions, with its current version being 0.8.3.","status":"active","version":"0.8.3","language":"en","source_language":"en","source_url":"https://github.com/scoder/fastrlock","tags":["threading","lock","rlock","performance","cython","concurrency","optimistic-lock"],"install":[{"cmd":"pip install fastrlock","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Requires CPython 3.7+; specific wheels for 0.8.3 support Python 3.13+ directly. Older versions may have less broad compatibility.","package":"python","optional":false},{"reason":"A core GNU C Library dependency, common on Linux systems.","package":"libc6","optional":false},{"reason":"Required for building from source distribution; not a runtime dependency when installing pre-built wheels.","package":"cython","optional":true}],"imports":[{"symbol":"rlock","correct":"from fastrlock import rlock"}],"quickstart":{"code":"import threading\nfrom fastrlock import rlock\n\ndef worker(lock, name):\n    print(f\"{name}: Trying to acquire lock...\")\n    with lock:\n        print(f\"{name}: Lock acquired. Doing work...\")\n        # Simulate re-entrant acquisition\n        with lock:\n            print(f\"{name}: Lock re-acquired internally.\")\n        print(f\"{name}: Work done, releasing lock.\")\n\n# Using fastrlock\nfast_lock = rlock()\nthread1 = threading.Thread(target=worker, args=(fast_lock, \"Thread A\"))\nthread2 = threading.Thread(target=worker, args=(fast_lock, \"Thread B\"))\n\nthread1.start()\nthread2.start()\n\nthread1.join()\nthread2.join()\nprint(\"All threads finished.\")","lang":"python","description":"Initialize and use `fastrlock.rlock()` as a direct replacement for `threading.RLock`. It supports the context manager protocol (`with lock:`)."},"warnings":[{"fix":"Benchmark your specific use case if performance is critical. Consider using the Cython C-API for maximum performance benefits if integrating with other Cython modules.","message":"Performance comparison with `threading.RLock` in Python 3.4+ may be mixed. While `fastrlock` is generally faster in Python 2.7 and through its Cython API, for Python 3.4 and later, the standard library's `threading.RLock` can be as fast or even faster when called through the Python API.","severity":"gotcha","affected_versions":"3.4+"},{"fix":"Upgrade to Python 3.7+ and use a recent version of `fastrlock` for official support and continued updates.","message":"Official Python 2 support has been removed, although Linux wheels for Python 2 might still be built for legacy reasons. New development focuses on Python 3.","severity":"deprecated","affected_versions":"<=0.7 (official support removed from 0.8+)"},{"fix":"Ensure you are using `fastrlock` version 0.7 or later when developing for Python 3.7 and above.","message":"Version 0.7 included an adaptation for unsigned thread IDs, which are used by Python 3.7 and newer. While primarily an internal change, using versions prior to 0.7 with Python 3.7+ could potentially lead to unexpected behavior related to thread identification.","severity":"breaking","affected_versions":"<0.7 with Python 3.7+"},{"fix":"If integrating with Cython code, leverage `from fastrlock cimport rlock` for optimal performance. Otherwise, carefully consider if the Python API provides sufficient benefits for your specific workload.","message":"The primary performance benefit of `fastrlock` in modern Python versions (3.4+) is realized when utilizing its C-API directly from other Cython modules, as this avoids Python call overhead. Using it purely through its Python API might yield less significant or no performance gains compared to the standard library's `threading.RLock`.","severity":"gotcha","affected_versions":"All versions on Python 3.4+"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}