asyncutils.rwlocks¶
Readers-writer locks with different fairness policies, applicable in different situations.
Classes¶
Interpret any callable as a regular function in a class body so that access on instance returns something like a bound method. |
|
At the same priority level, writers are preferred over readers. |
|
Readers-writer lock that maintains FIFO order for both readers and writers indiscriminately. |
|
Common base class for all readers-writer locks. |
|
Readers-writer lock preferring readers, which risks writer starvation. |
|
Writers at any priority level are preferred over readers at any priority level. |
|
Readers-writer lock preferring writers, which risks reader starvation. |
Module Contents¶
- class asyncutils.rwlocks.AgingRWLock[source]¶
Bases:
PriorityRWLockA readers-writer lock with a priority policy multiplying the current number of attempts to acquire the reading or writing lock,counted per corresponding task, by the respective factor given at construction, to obtain the priority.rf, the read priority factor, defaults tocontext.AGING_RWLOCK_DEFAULT_READ_PRIORITY_FACTOR.wf, the write priority factor, defaults tocontext.AGING_RWLOCK_DEFAULT_WRITE_PRIORITY_FACTOR.
- reading(priority: int = ...) asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writing(priority: int = ...) asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- class asyncutils.rwlocks.CoercedMethod[T, R, **P](func: collections.abc.Callable[Concatenate[R, P], T], /)[source]¶
Interpret any callable as a regular function in a class body so that access on instance returns something like a bound method.
- __get__(instance: R, owner: type[R] | None = ..., /) collections.abc.Callable[P, T][source]¶
Return the ‘bound method’. The descriptor itself is hidden and cannot be accessed on the class it is defined in, only instances of.
- class asyncutils.rwlocks.FairPriorityRWLock[source]¶
Bases:
PriorityRWLockAt the same priority level, writers are preferred over readers.
Whether instantiating this class gives a
PriorityRWLockunfair to readers by default depends oncontext.RWLOCK_DEFAULT_PREFER_WRITERS.
- class asyncutils.rwlocks.FairRWLock[source]¶
Bases:
RWLockReaders-writer lock that maintains FIFO order for both readers and writers indiscriminately.
Return a
WritePreferredRWLockifprefer_writersisTrue, otherwise aReadPreferredRWLock.context.RWLOCK_DEFAULT_PREFER_WRITERSbecomes the value ofprefer_writerswhen it is not passed.- reading() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writing() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- class asyncutils.rwlocks.PriorityRWLock[source]¶
Bases:
RWLockCommon base class ofFairPriorityRWLockandWritePreferredPriorityRWLock.Lower priority levels are preferred, and the default priority is 0, as in other patterns in this module related to priority.Whether instantiating this class gives a
PriorityRWLockunfair to readers by default depends oncontext.RWLOCK_DEFAULT_PREFER_WRITERS.- reading(priority: int = ...) asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writing(priority: int = ...) asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- class asyncutils.rwlocks.RWLock[source]¶
Bases:
abc.ABCCommon base class for all readers-writer locks.
Return a
WritePreferredRWLockifprefer_writersisTrue, otherwise aReadPreferredRWLock.context.RWLOCK_DEFAULT_PREFER_WRITERSbecomes the value ofprefer_writerswhen it is not passed.- classmethod lock[T, **P](f: collections.abc.Callable[P, collections.abc.Awaitable[T]], /) asyncutils._internal.types.RWLockRV[T, P][source]¶
Create a lock and register a reader.
- reader[T, **P](f: collections.abc.Callable[P, collections.abc.Awaitable[T]], /) asyncutils._internal.types.RWLockRV[T, P]¶
A decorator wrapping a function returning an awaitable in an async reading context.
reader()andwriter()methods are attached to the returned async callable.
- abstractmethod reading() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writer[T, **P](f: collections.abc.Callable[P, collections.abc.Awaitable[T]], /) asyncutils._internal.types.RWLockRV[T, P]¶
A decorator wrapping a function returning an awaitable in an async writing context.
reader()andwriter()methods are attached to the returned async callable.
- abstractmethod writing() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- class asyncutils.rwlocks.ReadPreferredRWLock[source]¶
Bases:
RWLockReaders-writer lock preferring readers, which risks writer starvation.
Return a
WritePreferredRWLockifprefer_writersisTrue, otherwise aReadPreferredRWLock.context.RWLOCK_DEFAULT_PREFER_WRITERSbecomes the value ofprefer_writerswhen it is not passed.- reading() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writing() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- class asyncutils.rwlocks.WritePreferredPriorityRWLock[source]¶
Bases:
PriorityRWLockWriters at any priority level are preferred over readers at any priority level.
Whether instantiating this class gives a
PriorityRWLockunfair to readers by default depends oncontext.RWLOCK_DEFAULT_PREFER_WRITERS.
- class asyncutils.rwlocks.WritePreferredRWLock[source]¶
Bases:
RWLockReaders-writer lock preferring writers, which risks reader starvation.
Return a
WritePreferredRWLockifprefer_writersisTrue, otherwise aReadPreferredRWLock.context.RWLOCK_DEFAULT_PREFER_WRITERSbecomes the value ofprefer_writerswhen it is not passed.- reading() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for reading access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.
- writing() asyncutils._internal.types.RWLockCM[source]¶
Return an async context manager for writing access. It is recommended to implement this by decorating an async generator function with
@contextlib.asynccontextmanager.