Fast Re-entrant Optimistic Lock

0.8.3 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.")

view raw JSON →