hbutils.concurrent.readwrite
This module provides a read-write lock implementation for controlling concurrent access to shared resources.
The ReadWriteLock class implements a reader-writer lock that allows multiple concurrent readers or a single exclusive writer, following these rules:
Read-Read: Non-exclusive, allows concurrent access
Read-Write: Exclusive, write operations wait for all read operations to complete
Write-Write: Exclusive, write operations execute serially
Write-Read: Exclusive, read operations wait for write operations to complete
This is useful for scenarios where you have frequent read operations and occasional write operations, allowing better performance through concurrent reads while maintaining data consistency.
ReadWriteLock
- class hbutils.concurrent.readwrite.ReadWriteLock(lock_factory: ~typing.Callable = <built-in function allocate_lock>)[source]
A read-write lock implementation for controlling concurrent access to shared resources.
This class implements a reader-writer lock that allows multiple concurrent readers or a single exclusive writer. The lock follows these exclusion rules:
Read-Read: Non-exclusive, multiple readers can access concurrently
Read-Write: Exclusive, write operations must wait for all read operations to complete
Write-Write: Exclusive, write operations execute serially
Write-Read: Exclusive, read operations must wait for write operations to complete
The lock is designed to optimize scenarios with frequent read operations and occasional write operations, providing better performance through concurrent reads while maintaining data consistency during writes.
- Parameters:
lock_factory (Callable) – Factory function to create lock objects, defaults to threading.Lock
- __init__(lock_factory: ~typing.Callable = <built-in function allocate_lock>)[source]
Initialize the ReadWriteLock.
- Parameters:
lock_factory (Callable) – Factory function to create lock objects, defaults to threading.Lock
- acquire_read() None[source]
Acquire a read lock.
This method allows multiple threads to acquire read locks concurrently. The first reader will block any potential writers by acquiring the write lock. Subsequent readers can proceed without blocking as long as no writer is waiting.
- acquire_write() None[source]
Acquire a write lock.
This method provides exclusive access for write operations. It will block until all current readers have finished and no other writers are active. Once acquired, no new readers or writers can proceed until the write lock is released.
- read_lock() Generator[None, None, None][source]
Context manager for read lock operations.
This method provides a convenient way to acquire and automatically release a read lock using the ‘with’ statement. The lock is guaranteed to be released even if an exception occurs within the context.
- Returns:
Generator for context management
- Return type:
Generator[None, None, None]
- Example:
>>> rwlock = ReadWriteLock() >>> with rwlock.read_lock(): ... # Perform read operations ... data = shared_resource.read()
- release_read() None[source]
Release a read lock.
This method decrements the reader count and releases the write lock when the last reader finishes, allowing pending write operations to proceed.
- Raises:
RuntimeError – If there are no active readers to release
- release_write() None[source]
Release a write lock.
This method releases the exclusive write lock, allowing pending readers and writers to proceed according to the lock’s scheduling policy.
- write_lock() Generator[None, None, None][source]
Context manager for write lock operations.
This method provides a convenient way to acquire and automatically release a write lock using the ‘with’ statement. The lock is guaranteed to be released even if an exception occurs within the context.
- Returns:
Generator for context management
- Return type:
Generator[None, None, None]
- Example:
>>> rwlock = ReadWriteLock() >>> with rwlock.write_lock(): ... # Perform write operations ... shared_resource.write(new_data)