asyncutils.mixins¶
Mixins classes for some common or specialized patterns that provide methods based on some abstract methods.
Classes¶
A subclass that implements the async |
|
Version of |
|
A mixin to derive |
|
Mixin for locks that can report their owner (the task currently holding it). |
|
Like |
Module Contents¶
- class asyncutils.mixins.AsyncContextMixin[T][source]¶
Bases:
abc.ABC__enter__()is optional and returns self by default, but that cannot be typed accurately.- async __aexit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) bool | None[source]¶
- async __aexit__(exc_typ: None, exc_val: None, exc_tb: None, /) Literal[False] | None
- abstractmethod __exit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) bool | None[source]¶
- abstractmethod __exit__(exc_typ: None, exc_val: None, exc_tb: None, /) Literal[False] | None
- class asyncutils.mixins.AwaitableMixin[T][source]¶
Bases:
abc.ABCA subclass that implements the async
wait()method automatically becomes awaitable, resolving to the return value of that method.- __await__() collections.abc.Generator[Any, None, T][source]¶
Await statement support.
- abstractmethod wait() collections.abc.Awaitable[T][source]¶
- class asyncutils.mixins.EventMixin[T][source]¶
Bases:
AwaitableMixin[T],asyncutils._internal.helpers.LoopMixinBase,abc.ABCMixin for event classes that don’t inherit fromasyncio.Eventbut provide enhanced functionality with the same API and some mixinmethods, most notably making the event itself awaitable. This is simply syntactic sugar for calling the wait method, but more convenient andintuitive when thinking of events as reusable futures.- abstractmethod get() T[source]¶
Return the value currently held by the event, or raise an exception if there is no value set. The implementations in this library have a dedicated exception type,
EventValueError, for this.
- abstractmethod set(value: T) None[source]¶
Set the value of the event and wake up all waiters. Implementations may not choose to clear the value within this method.
- async wait(timeout: float | None = ...) T[source]¶
Wait for the event with an optional
timeoutand return its value.
- abstractmethod wait_for_next(timeout: float | None = ...) T[source]¶
- Async:
Wait for the next time the event is set, and return the value it was set to. Should always block even if there is currently a value set.
- async wait_for_value(val: T, timeout: float | None = ..., *, set_at_timeout: bool = ...) None[source]¶
Wait for the event to be set to a specific value
val, with an optionaltimeout. Ifset_at_timeoutisTrue, the event will be set tovalwhen the timeout occurs, and waiters will be woken up. The value will persist on the event by default in this case.
- class asyncutils.mixins.ExecutorRequiredAsyncContextMixin[T][source]¶
Bases:
abc.ABCVersion of
AsyncContextMixinthat uses an executor to convert the sync context manager methods to async in an event loop.- async __aexit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) bool | None[source]¶
- async __aexit__(exc_typ: None, exc_val: None, exc_tb: None, /) Literal[False] | None
- abstractmethod __exit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) bool | None[source]¶
- abstractmethod __exit__(exc_typ: None, exc_val: None, exc_tb: None, /) Literal[False] | None
- property runner: collections.abc.Callable[[collections.abc.Callable[[*Ts], R], *Ts], asyncio.Future[R]][source]¶
- class asyncutils.mixins.LockMixin[T][source]¶
Bases:
abc.ABCA mixin to derive
__aenter__()and__aexit__()fromacquire()andrelease()of subclasses.- async __aexit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) None[source]¶
- async __aexit__(exc_typ: None, exc_val: None, exc_tb: None, /) None
Release the lock and optionally perform handling according to the exception occurred.
- classmethod __init_subclass__(*, _lock_factory_: collections.abc.Callable[[Self], T] = ..., **k: object) None[source]¶
- acknowledge_locksmith_lock_held(
- smith: asyncutils.locksmiths.LocksmithBase,
- /,
Optional method to cooperate with locksmiths in case of unusually long wait times for the lock which may indicate deadlock, livelock or starvation.
- abstractmethod acquire() bool[source]¶
- Async:
Acquire the lock, waiting if necessary. Return whether the lock was acquired successfully. Some locks may raise an error if not; that is up to the implementation.
- abstractmethod locked() bool[source]¶
Return whether the lock is currently held by a task satisfying a certain criterion (e.g. it is the task awaited the acquiry).
- abstractmethod release() collections.abc.Coroutine[Any, Any, None] | None[source]¶
Release the lock. Some locks may require this to be called from a task that holds the lock, or may raise an error if the lock is not currently held; that is up to the implementation.
- class asyncutils.mixins.LockWithOwnerMixin[R: (None, collections.abc.Coroutine[Any, Any, None])][source]¶
-
Mixin for locks that can report their owner (the task currently holding it).
- abstractmethod _release() R[source]¶
Will be wrapped by
release()to throwRuntimeErrorif the current task is not the owner of the lock.
- class asyncutils.mixins.LoopContextMixin[source]¶
Bases:
asyncutils._internal.helpers.LoopMixinBaseLike
asyncio.TaskGroup, but manages an event loop publicly, allows custom setup and teardown logic and waits for the cancellations to complete on exit.- async __aenter__() Self[source]¶
Call
__setup__()and return self.
- async __aexit__(exc_typ: asyncutils._internal.types.ExcType, exc_val: BaseException, exc_tb: types.TracebackType, /) None[source]¶
- async __aexit__(exc_typ: None, exc_val: None, exc_tb: None, /) None
Call
__cleanup__()and cancel all running tasks in the loop.
- property loop: asyncio.AbstractEventLoop¶
The underlying event loop.
- property running_tasks: set[asyncio.Task[Any]]¶
A set of all tasks currently running in the underlying loop.