asyncutils.compete¶
Functions¶
|
|
|
Version of |
|
|
|
Return a list of all the coroutines that completed within |
|
Module Contents¶
- asyncutils.compete.convert_to_coro_iter(
- cfs: asyncutils._internal.types.SupportsIteration[Any],
- *,
- skip_invalid: bool = ...,
- loop: asyncio.AbstractEventLoop | None = ...,
- corocheck: collections.abc.Callable[[Any], TypeGuard[collections.abc.Coroutine[Any, Any, Any]]] = ...,
- futwrap: asyncutils._internal.types.FutWrapType = ...,
- handle_aiter: collections.abc.Callable[[collections.abc.AsyncIterable[Any]], collections.abc.Coroutine[Any, Any, Any]] = ...,
- handle_iter: collections.abc.Callable[[collections.abc.Iterable[Any]], collections.abc.Coroutine[Any, Any, Any]] = ...,
- A helper function to convert a possibly async iterable of futures, coroutines and even (async) iterables
cfsto a plain generator ofcoroutines, such that it may be starred and passed into the functions in this module.Originally designed to complementstaggered_race().Due to the possibility ofcfsbeing async and this function being designed to operate in a sync context, it is somewhat inefficient.skip_invalid, which determines whether to raiseTypeErrorfor unconvertible items or simply to skip them, defaults tocontext.CONVERT_TO_CORO_ITER_DEFAULT_SKIP_INVALID.handle_aiterandhandle_itershould be callables taking an async iterable and a sync iterable respectively and returning a coroutine.
- async asyncutils.compete.enhanced_gather(
- it: asyncutils._internal.types.SupportsIteration[Any],
- return_exceptions: bool = False,
- *,
- loop: asyncio.AbstractEventLoop | None = ...,
Version of
asyncio.gather()that takes a larger variety of objects as the first argument, usingconvert_to_coro_iter()under the hood.See also
iters.agather()if you just want to pass in an async iterable; this version materializes a list of all the items within but avoids creating the intermediate sync futures, which in many cases is a better strategy
- async asyncutils.compete.enhanced_staggered_race(
- cfs: asyncutils._internal.types.SupportsIteration[Any],
- delay: float | None = ...,
- *,
- loop: asyncio.AbstractEventLoop | None = ...,
asyncio.staggered.staggered_race(), but taking a larger variety of objects as the first argument usingconvert_to_coro_iter(); see above.
- async asyncutils.compete.first_completed[T](
- *C: collections.abc.Awaitable[T],
- ret_exc: Literal[True],
- timeout: float | None = ...,
- async asyncutils.compete.first_completed(*C: collections.abc.Awaitable[T], ret_exc: Literal[False] = ..., timeout: float | None = ...) T | None
- Return the result of the first coroutine that completes among those passed in.If
ret_excisTrue, the coroutine might have errored, in which case the exception it throws is returned in a wrapped formunpackable usingexceptions.unwrap_exc()after checking withexceptions.exception_occurred().In any case, the losing coroutines are cancelled together and the function returns when the cancellations finish.
- async asyncutils.compete.multi_winner_race_with_callback[T](
- *C: collections.abc.Awaitable[T],
- timeout: float,
- winner: collections.abc.Callable[[T], object] = ...,
- loser: collections.abc.Callable[[Any | BaseException], object] = ...,
Return a list of all the coroutines that completed within
timeout, and cancel the rest, triggering callbacks similarly torace_with_callback().
- async asyncutils.compete.race_with_callback[T](
- *C: collections.abc.Awaitable[T],
- winner: collections.abc.Callable[[T], object] = ...,
- loser: collections.abc.Callable[[Any | BaseException], object] = ...,
- timeout: float | None = ...,
- Return the result of the first coroutine to complete, which will have
winnercalled on it.If no coroutine completes withintimeout,Noneis returned.Thelosercallback is called on each return value of or exception raised by the losing coroutines after seeingCancelledError.