Skip to content

Thread lock API

ReaderWriterLock

Bases: object

An inter-thread readers writer lock.

Attributes

has_pending_writers: bool property

Check if there pending writers

Returns:

Type Description
bool

Whether there are pending writers.

owner: Optional[str] property

Caller ownership (if any) of the lock

Returns:

Type Description
Optional[str]

'w' if caller is a writer, 'r' if caller is a reader, None otherwise.

Functions

__init__(condition_cls=threading.Condition, current_thread_functor=threading.current_thread)

Parameters:

Name Type Description Default
condition_cls

Optional custom Condition primitive used for synchronization.

Condition
current_thread_functor

Optional function that returns the identity of the thread in case threads are not properly identified by threading.current_thread

current_thread

is_writer(check_pending=True)

Check if caller is a writer (optionally pending writer).

Parameters:

Name Type Description Default
check_pending bool

Whether to check for pending writer status.

True

Returns:

Type Description
bool

Whether the caller is the active (or optionally pending) writer.

is_reader()

Check if caller is a reader.

Returns:

Type Description
bool

Whether the caller is an active reader.

read_lock()

Context manager that grants a read lock.

Will wait until no active or pending writers.

Raises:

Type Description
RuntimeError

if a pending writer tries to acquire a read lock.

write_lock()

Context manager that grants a write lock.

Will wait until no active readers. Blocks readers after acquiring.

Guaranteed for locks to be processed in fair order (FIFO).

Raises:

Type Description
RuntimeError

if an active reader attempts to acquire a lock.