Documentation Index
Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt
Use this file to discover all available pages before exploring further.
RLock is a reentrant lock that supports both synchronous (threading) and asynchronous (asyncio) contexts from a single object. It tracks ownership per-thread for sync code and per-task for async code, and uses FIFO queues so that waiting callers are woken in the order they arrived.
See the author’s article for an in-depth explanation of the design.
Initialisation
threading.RLock) and async (asyncio.Lock) primitives along with their condition variables and FIFO queues.
Synchronous usage
UseRLock anywhere a standard threading.RLock would be used.
Sync methods
acquire() -> bool
Blocks until the lock is available for the current thread. Returns True on success. If the calling thread already owns the lock, the reentrancy counter is incremented immediately.
release() -> bool
Decrements the reentrancy counter. When it reaches zero the lock is released and waiting threads are notified. Raises RuntimeError if the calling thread does not own the lock.
Asynchronous usage
Useasync with or async_acquire / async_release inside async functions.
asyncio.Task can acquire the lock multiple times.
Async methods
async async_acquire() -> bool
Awaits until the lock is available for the current task. Returns True on success.
async async_release() -> bool
Decrements the async reentrancy counter. When it reaches zero, waiting tasks are notified via the condition variable. Raises RuntimeError if the current task does not own the lock.
Fairness
RLock uses collections.deque as a FIFO waiting queue for both sync and async contexts. New waiters are appended to the back of the queue; a waiter is only granted the lock when it is at the front. This prevents starvation under high contention.
