asyncutils.util¶
Functions of utility one tier below the base submodule, such that they are not worth preloading but still quite useful.
Attributes¶
An instance of an async context manager that does nothing. |
|
Context manager to ignore |
Functions¶
|
Equivalent to |
|
A decorator factory converting a function giving an awaitable resolving to an async context manager into a function returning a non-reusable dual context manager using |
|
An async function that does nothing and returns |
|
Return a copy of the async function |
|
Emulate the second form of the builtin |
|
An async function that does nothing and returns |
|
An async function that does nothing and returns |
|
Return an async function that takes any arguments, always returning the value |
|
Equivalent to |
|
Return a new async event that is already set, with type |
|
Return a future that is already done with the result |
Convert a callable that returns an (async) iterable, usually an (async) generator function, over exactly one item, into a function returning a non-reusable sync- and async-compatible context manager. Essentially combines |
|
|
|
|
Return an already acquired lock of type |
|
Apply a lock that implements the async lock interface, as constructed and returned by |
|
Yield eagerly started tasks wrapping the coroutines under the running loop (or a new one that is set as the current if required) in order. |
|
|
|
Simple helper function returning a (bounded) semaphore of value |
|
Synchronously await the awaitable object |
|
|
|
Convert a function that returns an awaitable to an sync function with the same signature, using the event loop |
|
A version of |
Return the partial of |
|
|
Return a coroutine resolving to the result of the awaitable |
Module Contents¶
- asyncutils.util.aawcmf2dcmf[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[contextlib.AbstractContextManager[T] | contextlib.AbstractAsyncContextManager[T]]],
- /,
Equivalent to
aawcmf2dcmff()(f).
- asyncutils.util.aawcmf2dcmff[
- T,
- **P,
A decorator factory converting a function giving an awaitable resolving to an async context manager into a function returning a non-reusable dual context manager using
dualcontextmanager().
- async asyncutils.util.afalsify(*a: Any, **k: Any) Literal[False]¶
An async function that does nothing and returns
False.
- asyncutils.util.afcopy[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[T]],
- /,
Return a copy of the async function
fwith the same signature and attributes.
- asyncutils.util.aiter_from_f[T](
- f: collections.abc.Callable[[], collections.abc.Awaitable[T]],
- sentinel: T = ...,
- /,
Emulate the second form of the builtin
iter()function in async, which theaiter()function does not have.
- async asyncutils.util.anullify(*a: Any, **k: Any) None¶
An async function that does nothing and returns
None.
- async asyncutils.util.atruthify(*a: Any, **k: Any) Literal[True]¶
An async function that does nothing and returns
True.
- asyncutils.util.avalify[T](v: T, /) collections.abc.Callable[Ellipsis, types.CoroutineType[Any, Any, T]][source]¶
Return an async function that takes any arguments, always returning the value
v.
- asyncutils.util.dcm[T, **P]( ) collections.abc.Callable[P, asyncutils._internal.types.DualContextManager[T]]¶
Equivalent to
dualcontextmanager()with the default arguments at the time of definition, rather than when the function is decorated.
- asyncutils.util.done_evt[E: asyncutils._internal.types.EventProt](*, evtcls: type[E]) E[source]¶
- asyncutils.util.done_evt() asyncio.Event
Return a new async event that is already set, with type
evtclsif passed andasyncio.Eventby default.
- asyncutils.util.done_fut(
- exc: asyncutils._internal.types.ExceptionWrapper,
- /,
- *,
- futcls: type[asyncutils._internal.types.FutProt[Any]],
- asyncutils.util.done_fut(
- res: None = ...,
- *,
- futcls: type[asyncutils._internal.types.FutProt[Any]],
- asyncutils.util.done_fut(res: T, *, futcls: type[asyncutils._internal.types.FutProt[Any]]) asyncutils._internal.types.IncompleteFut[T]
- asyncutils.util.done_fut(exc: asyncutils._internal.types.ExceptionWrapper, /) asyncio.Future[Never]
- asyncutils.util.done_fut(res: None = ...) asyncio.Future[None]
- asyncutils.util.done_fut(res: T) asyncio.Future[T]
Return a future that is already done with the result
resor the exception wrapped by the wrapperexcif it is an exception wrapper returned byexceptions.wrap_exc(), with typefutclsif passed andasyncio.Futureby default.
- asyncutils.util.dualcontextmanager[
- T,
- **P,
- *,
- use_existing_executor: bool,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager(
- *,
- create_executor: bool,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager( ) collections.abc.Callable[[collections.abc.Callable[P, collections.abc.Iterable[T]]], collections.abc.Callable[P, contextlib.AbstractContextManager[T, bool]]]
- asyncutils.util.dualcontextmanager(
- *,
- use_existing_executor: bool,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- *,
- create_executor: bool,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager( ) collections.abc.Callable[[collections.abc.Callable[P, collections.abc.Iterable[T]]], collections.abc.Callable[P, asyncutils._internal.types.DualContextManager[T]]]
- asyncutils.util.dualcontextmanager(*, strict: Literal[True]) asyncutils._internal.types.DCRV
- asyncutils.util.dualcontextmanager(
- *,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- *,
- strict: bool = ...,
- asyncutils.util.dualcontextmanager(
- gfunc: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager(
- gfunc: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- gfunc: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: bool = ...,
- asyncutils.util.dualcontextmanager(
- agfunc: collections.abc.Callable[P, collections.abc.AsyncIterable[T]],
- /,
- *,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager(
- agfunc: collections.abc.Callable[P, collections.abc.AsyncIterable[T]],
- /,
- *,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- agfunc: collections.abc.Callable[P, collections.abc.AsyncIterable[T]],
- /,
- *,
- strict: bool = ...,
Convert a callable that returns an (async) iterable, usually an (async) generator function, over exactly one item, into a function returning a non-reusable sync- and async-compatible context manager. Essentially combines
contextlib.contextmanager()andcontextlib.asynccontextmanager()into one decorator.
- asyncutils.util.get_future[T](aw: collections.abc.Awaitable[T], loop: asyncio.AbstractEventLoop | None = ...) asyncio.Future[T][source]¶
- Wrap an arbitrary awaitable
awin a task underloop, creating one and setting if required, and begin waiting on it.Critical exceptions are wrapped inCritical.This is as opposed tocreate_task(), which only takes coroutines.
- asyncutils.util.locked_lock[L: asyncutils._internal.types.AsyncLockLike[Any]](*, lcls: type[L]) L[source]¶
- asyncutils.util.locked_lock() asyncio.Lock
Return an already acquired lock of type
lclsif passed andasyncio.Lockby default.
- asyncutils.util.lockf[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[T]],
- /,
- lf: type[asyncutils._internal.types.AsyncLockLike[Any]] = ...,
Apply a lock that implements the async lock interface, as constructed and returned by
lf, to a functionfthat returns an awaitable, also converting it to an async function.
- asyncutils.util.new_eager_tasks[T](*aws: collections.abc.Awaitable[T]) collections.abc.Generator[asyncio.Task[T]][source]¶
Yield eagerly started tasks wrapping the coroutines under the running loop (or a new one that is set as the current if required) in order.
- async asyncutils.util.safe_cancel(fut: asyncio.Future[Any], /) None[source]¶
- Cancel a single future and wait for the cancellation to complete asynchronously.Advertises itself as safe, because the cancellation itself can be reliably cancelled.
See also
safe_cancel_batch()a much more efficient way to cancel multiple futures at once without compromising cancellability.
- asyncutils.util.semaphore(bounded: Literal[False] = ..., workers: int = ...) asyncio.Semaphore[source]¶
- asyncutils.util.semaphore(bounded: Literal[True], workers: Literal[1]) asyncio.Lock
- asyncutils.util.semaphore(bounded: Literal[True], workers: int = ...) asyncio.BoundedSemaphore
Simple helper function returning a (bounded) semaphore of value
workers, defaulting tocontext.SEMAPHORE_DEFAULT_VALUE.
- asyncutils.util.sync_await[T](
- aw: collections.abc.Awaitable[T],
- loop: asyncio.AbstractEventLoop | None = ...,
- *,
- never_block: bool = ...,
- timeout: float | None = ...,
Synchronously await the awaitable object
awunder the given event looploopwith timeouttimeout. Ifnever_block=Falseis passed and the loop is not running, itsrun_until_complete()method may be called; otherwise, a pair of futures is created to coordinate the execution of the awaitable. It is preferred to useasyncio.run()to synchronously run one single top-level async function that awaits the necessary awaitables. Calling this function with the event loop running in the current thread will causeRuntimeErrorto be thrown.
- asyncutils.util.to_async[T, **P](f: collections.abc.Callable[P, T], /) collections.abc.Callable[P, types.CoroutineType[Any, Any, T]][source]¶
- Return the async version of the original function with all the attributes from its instance dictionary, which runs in an executor lazyinitialized and shared by all
to_async()-transformed callables.If the argument was returned byto_sync(), a copy of the original async function is returned.Warning
This function may create reference cycles. If memory is a concern, call
gc.collect()regularly.See also
pools.AdvancedPoolan async-first thread pool executor-like class.
- asyncutils.util.to_sync[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[T]],
- /,
- loop: asyncio.AbstractEventLoop | None = ...,
- *,
- timeout: float | None = ...,
Convert a function that returns an awaitable to an sync function with the same signature, using the event loop
loopwhen required or creating when necessary.
- asyncutils.util.to_sync_from_loop(loop: asyncio.AbstractEventLoop) asyncutils._internal.types.ToSyncFromLoopRV[source]¶
A version of
to_sync()that is a decorator factory, returning its partial underloop=loop.
- asyncutils.util.transient_block[T, **P](loop: asyncio.AbstractEventLoop, f: collections.abc.Callable[P, T], /, *a: P, **k: P) asyncio.Future[T][source]¶
- asyncutils.util.transient_block(
- loop: asyncio.AbstractEventLoop,
- f: collections.abc.Callable[Ellipsis, T],
- /,
- *a: object,
- _threadsafe_: Literal[True],
- **k: object,
- Run a sync function
f, with the provided parameters passed straight through, in the event looploop, and return an async futureresolving to its result or exception.This function avoids incurring the overhead of callingrun_in_executor()by instead scheduling the function to run at thenext iteration of the loop. To avoid overhead, the function should return fast.If_threadsafe_isTrue, then the function is scheduled in a thread-safe way, so that this can be called from threads not owning the loop.
- asyncutils.util.transient_block_from_loop(
- loop: asyncio.AbstractEventLoop,
- *,
- threadsafe: bool = ...,
Return the partial of
transient_block()under the specifiedloop.
- async asyncutils.util.wrap_in_coro[T](aw: collections.abc.Awaitable[T], /) T[source]¶
Return a coroutine resolving to the result of the awaitable
aw, such that it can be passed toasyncio.create_task().
- asyncutils.util.anullcontext: asyncutils._internal.types.ANCT¶
An instance of an async context manager that does nothing.
- asyncutils.util.ignore_cancellation: asyncutils.exceptions.IgnoreErrors¶
Context manager to ignore
CancelledError.