Registry / database / fastrlock

fastrlock

JSON →
library0.8.3pypypi✓ verified 26d ago

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.

pip install fastrlock
INSTALL
IMPORT
SIG · FASTRLOCK
F
fastrlock
databasepythonv0.8.3
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.8.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rlock
from fastrlock import rlock

Initialize and use `fastrlock.rlock()` as a direct replacement for `threading.RLock`. It supports the context manager protocol (`with lock:`).

import threading from fastrlock import rlock def worker(lock, name): print(f"{name}: Trying to acquire lock...") with lock: print(f"{name}: Lock acquired. Doing work...") # Simulate re-entrant acquisition with lock: print(f"{name}: Lock re-acquired internally.") print(f"{name}: Work done, releasing lock.") # Using fastrlock fast_lock = rlock() thread1 = threading.Thread(target=worker, args=(fast_lock, "Thread A")) thread2 = threading.Thread(target=worker, args=(fast_lock, "Thread B")) thread1.start() thread2.start() thread1.join() thread2.join() print("All threads finished.")
Debug
Known issues
gotchaPerformance 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.
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.
affects: 3.4+
deprecatedOfficial 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.
fix
Upgrade to Python 3.7+ and use a recent version of `fastrlock` for official support and continued updates.
affects: <=0.7 (official support removed from 0.8+)
breakingVersion 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.
fix
Ensure you are using `fastrlock` version 0.7 or later when developing for Python 3.7 and above.
affects: <0.7 with Python 3.7+
gotchaThe 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`.
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.
affects: All versions on Python 3.4+
Errors
Common errors & fixes
error: Microsoft Visual C++ 14.0 is required.
This error occurs on Windows when attempting to install fastrlock via pip, as it is a Cython-implemented library that requires compilation with the Microsoft Visual C++ Build Tools.
fix
Install the 'Desktop development with C++' workload, including the 'MSVC v143 - VS 2022 C++ x64/x86 build tools' (or equivalent for your Visual Studio version) from the Visual Studio Installer, or download the Microsoft Visual C++ Build Tools separately from the official Visual Studio website.
ModuleNotFoundError: No module named 'fastrlock'
This error indicates that the 'fastrlock' package is not installed in the Python environment or Python cannot find it in its search path.
fix
Ensure fastrlock is correctly installed by running `pip install fastrlock`. If using a virtual environment, ensure it's activated, and the installation is successful.
ImportError: cannot import name 'RLock' from 'fastrlock'
Users attempting to import `RLock` (with a capital 'R') directly from the `fastrlock` package will encounter this error because the main re-entrant lock class provided by the library is named `FastRLock`, or can be imported as `rlock` (lowercase) for direct use.
fix
Import the correct class as `from fastrlock import FastRLock` to use the class directly, or `from fastrlock import rlock` if you're importing the module-level instance, and then use `FastRLock()` or `rlock` respectively.
TypeError: 'module' object is not callable
This error happens when a user tries to call the `fastrlock` module itself as if it were a function or class, instead of instantiating the `FastRLock` class within it.
fix
To create a lock, you need to instantiate the `FastRLock` class. The correct way is `from fastrlock import FastRLock; lock = FastRLock()` or `import fastrlock; lock = fastrlock.FastRLock()`.
Upgrade
Version history
0.8.3latest on PyPI · released Dec 17, 2024
Audit
Dependencies
pythonrequiredRequires CPython 3.7+; specific wheels for 0.8.3 support Python 3.13+ directly. Older versions may have less broad compatibility.
libc6requiredA core GNU C Library dependency, common on Linux systems.
cythonoptionalRequired for building from source distribution; not a runtime dependency when installing pre-built wheels.
Agent activity
13 hits · last 30 days
node
12
Resources
fastrlock — pip install fastrlock · libregistry