asyncutils.locks¶
asyncio.asyncio.Lock and made explicit in theAsyncLockLike protocol, besides MultiCountDownLatch, since it usesKeyedCondition internally and it is not desired for asyncutils.altlocks to import this submodule as well.Classes¶
A rate limiter that supports a mode in which waiters can cut the queue. |
|
A subclass of |
|
A condition variable that allows waiting on and notifying individual keys, or all keys at once. |
|
A collection of count-down latches, each identified by a key, supporting waiting on individual latches or on all of them at once. |
|
A lock allowing waiters with a lower priority value to enter first. |
|
A reentrant lock supporting priority. |
|
A semaphore that allows waiters with a lower priority value to enter first. |
|
An async reentrant lock that is somehow missing from |
Module Contents¶
- class asyncutils.locks.AdvancedRateLimit(rate: float, capacity: float = ..., fair: bool = ...)[source]¶
Bases:
asyncutils._internal.helpers.LoopMixinBase,asyncutils.mixins.LockMixin[None]A rate limiter that supports a mode in which waiters can cut the queue.
rate(required): The initial rate at which tokens refill.capacity: The maximum rate, defaulting to the current rate.fair: Whether to maintain FIFO (first in, first out) for waiters; defaultTrue.- async acquire(tokens: float = ..., timeout: float | None = ...) Literal[True][source]¶
Acquire the specified number of tokens from the rate limiter (default
context.ADVANCED_RATE_LIMIT_DEFAULT_TOKENS), waiting until the timeout expires and signallingTimeoutErrorif necessary.
- locked() bool[source]¶
Return
Trueif the limiter is currently locked, such thatacquire()must block to wait for tokens.
- async release(tokens: float = ...) None[source]¶
Release the specified number of tokens back to the rate limiter (default
context.ADVANCED_RATE_LIMIT_DEFAULT_TOKENS).
- class asyncutils.locks.DynamicBoundedSemaphore(value: int = ...)[source]¶
Bases:
asyncio.BoundedSemaphoreA subclass of
asyncio.BoundedSemaphorewhose bound can be set by the user via theboundproperty.value, the initial value of the semaphore, defaults tocontext.DYNAMIC_BOUNDED_SEMAPHORE_DEFAULT_VALUE.
- class asyncutils.locks.KeyedCondition[T](lock: asyncio.Lock | asyncutils.mixins.LockMixin[Any] | None = ...)[source]¶
Bases:
asyncutils.mixins.LockMixin[KeyedCondition[T]],asyncutils.mixins.LoopContextMixinA condition variable that allows waiting on and notifying individual keys, or all keys at once.
Initialize the condition variable with the given lock, or create a new one if not passed.
- async acquire() bool[source]¶
Wrap the acquire method of the underlying lock to only reraise critical errors and return success.
- assert_locked() None[source]¶
Assert that the underlying lock is currently locked, and raise a
RuntimeErrorif not.
- notify(key: T, n: int = ..., strict: bool = ...) None[source]¶
Notify
nwaiters waiting on the given keykey(default 1). IfstrictisTrueand the key doesn’t exist,KeyErroris raised.
- notify_all(key: T | None = ...) int[source]¶
Notify all waiters waiting on the given key
key, or all waiters ifkeyisNone. Return the number of waiters that were notified.
- async wait(key: T, timeout: float | None = ...) None[source]¶
Wait for the given key
keyto be notified withintimeout.
- class asyncutils.locks.MultiCountDownLatch[H: collections.abc.Hashable](counts: collections.abc.Mapping[H, int])[source]¶
A collection of count-down latches, each identified by a key, supporting waiting on individual latches or on all of them at once.
Initialize the latch with the given mapping of keys to counts. No more keys can be added after this stage.
- async count_down(key: H, strict: bool = ...) None[source]¶
Decrement the count of the latch with the given key by one. If it reaches zero, wake up all waiters. If
strictisTrueand the key doesn’t exist,KeyErroris raised.
- async wait(key: H, strict: bool = ...) None[source]¶
Wait for the latch with the given key to reach zero. If
strictisTrueand the key doesn’t exist,KeyErroris raised.
- async wait_all(timeout: float | None = ...) None[source]¶
Wait for the count of all latches to reach zero.
- property broken: bool¶
If this returns
True, it means thatwait_all()will return immediately.
- class asyncutils.locks.PriorityLock[source]¶
Bases:
asyncutils._internal.helpers.LoopMixinBase,asyncutils.mixins.LockWithOwnerMixin[None]A lock allowing waiters with a lower priority value to enter first.
- class asyncutils.locks.PriorityRLock[source]¶
Bases:
RLockA reentrant lock supporting priority.
- property owner: asyncio.Task[Any] | None¶
- class asyncutils.locks.PrioritySemaphore(value: int = ...)[source]¶
Bases:
asyncutils._internal.helpers.LoopMixinBase,asyncutils.mixins.LockMixin[None]A semaphore that allows waiters with a lower priority value to enter first.
value, the initial value as an integer, defaults tocontext.PRIORITY_SEMAPHORE_DEFAULT_VALUE.- async acquire(priority: int = ...) Literal[True][source]¶
Acquire the semaphore with the specified priority (default
context.PRIORITY_SEMAPHORE_DEFAULT_PRIORITY).
- release(strict: bool = ...) None[source]¶
Release the semaphore. If
strictisTrue(the default) and the number of releases is more than the number of acquisitions, aRuntimeErroris raised.
- class asyncutils.locks.RLock(lock: asyncutils._internal.types.AsyncContextManager[Any] | None = ...)[source]¶
Bases:
asyncutils.mixins.LockWithOwnerMixin[None]An async reentrant lock that is somehow missing from
asyncio.