asyncutils.util¶
Functions of utility one tier below base, 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 |
|
Return a decorator converting a function giving an awaitable resolving to an async context manager into a function returning a non-reusable dual context manager using |
|
Do nothing and return |
|
Return a copy of the async function |
|
Emulate the second form of the builtin |
|
Do nothing and return |
|
Do nothing and return |
|
Return an async function that takes any arguments, always returning the value |
|
Equivalent to |
|
Return an async function with the same signature as |
|
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 async function with the same signature as |
|
|
|
Return an already acquired lock of type |
|
Apply a lock that implements the async lock interface, as constructed and returned by |
|
Return a task factory accepted 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. |
|
|
|
Return a (bounded) semaphore of value |
|
Await the awaitable object |
|
|
|
Convert a function that returns an awaitable to an sync function with the same signature, using the event loop |
|
Return the partial 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,
Return a decorator 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: object, **k: object) Literal[False]¶
Do nothing and return
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 = ...,
- /,
- *,
- yield_sentinel: bool = ...,
Emulate the second form of the builtin
iter()function in async, which theaiter()function does not have.
- 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.prots.DualContextManager[T]]¶
Equivalent to
dualcontextmanager()with the default arguments at the time of definition, rather than when the function is decorated.
- asyncutils.util.discard_retval[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[T]],
- /,
Return an async function with the same signature as
fthat awaits the result offand discards it.
- asyncutils.util.done_evt[T: asyncutils._internal.prots.EventProtocol](*, evtcls: type[T]) T[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.prots.ExceptionWrapper,
- /,
- *,
- futcls: type[asyncutils._internal.prots.FutProtocol[Any]],
- asyncutils.util.done_fut(
- res: None = ...,
- *,
- futcls: type[asyncutils._internal.prots.FutProtocol[Any]],
- asyncutils.util.done_fut(
- res: T,
- *,
- futcls: type[asyncutils._internal.prots.FutProtocol[Any]],
- asyncutils.util.done_fut(exc: asyncutils._internal.prots.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 bywrap_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.prots.DualContextManager[T]]]
- asyncutils.util.dualcontextmanager(*, strict: Literal[True]) asyncutils._internal.prots.StrictDualContextFactory
- asyncutils.util.dualcontextmanager(
- *,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- *,
- strict: bool = ...,
- asyncutils.util.dualcontextmanager(
- genf: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager(
- genf: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- genf: collections.abc.Callable[P, collections.abc.Iterable[T]],
- /,
- *,
- use_existing_executor: bool = ...,
- create_executor: bool = ...,
- strict: bool = ...,
- asyncutils.util.dualcontextmanager(
- agenf: collections.abc.Callable[P, collections.abc.AsyncIterable[T]],
- /,
- *,
- strict: Literal[True],
- asyncutils.util.dualcontextmanager(
- agenf: collections.abc.Callable[P, collections.abc.AsyncIterable[T]],
- /,
- *,
- strict: Literal[False],
- asyncutils.util.dualcontextmanager(
- agenf: 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.evaluate_and_return[T, **P](
- f: collections.abc.Callable[P, collections.abc.Awaitable[object]],
- r: T,
- /,
Return an async function with the same signature as
fthat awaits the result offand returnsr.
- 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.
- async asyncutils.util.locked_lock[T: asyncutils._internal.prots.AsyncLockLike[Any]](*, lcls: type[T]) T[source]¶
- async 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.prots.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.make_task_factory[T: asyncio.Task[Any]](tcls: type[T], eager: bool = ...) asyncutils._internal.prots.TaskFactory[T][source]¶
Return a task factory accepted by
set_task_factory()that creates tasks of typetclswith theeager_startargument set toeager, its default value beingMAKE_TASK_FACTORY_DEFAULT_EAGER.
- asyncutils.util.new_eager_tasks[T](*aws: collections.abc.Awaitable[T]) types.GeneratorType[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.The cancellation itself can be reliably cancelled, thus the name.
See also
safe_cancel_batch()a more efficient way to cancel multiple futures at once, utilizing somewhat structured concurrency.
- 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
Return a (bounded) semaphore of value
workers, defaulting toSEMAPHORE_DEFAULT_VALUE.
- asyncutils.util.sync_await[T](
- aw: collections.abc.Awaitable[T],
- loop: asyncio.AbstractEventLoop | None = ...,
- *,
- never_block: bool = ...,
- timeout: float | None = ...,
Await the awaitable object
awunder the given event looploopwith timeouttimeoutsynchronously. 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 lazy initialized 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
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.prots.ToSyncFromLoopRV[source]¶
Return the partial of
to_sync()underloop=loop.
- asyncutils.util.transient_block[T, **P](
- loop: asyncio.AbstractEventLoop,
- f: collections.abc.Callable[P, T],
- /,
- *a: P,
- **k: P,
- 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 future resolving to its result or exception.This function avoids incurring the overhead of callingrun_in_executor()by instead scheduling the function to run at the next iteration of the loop.To avoid overhead, only use this on functions that 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.prots.NullContextType¶
An instance of an async context manager that does nothing.
- asyncutils.util.ignore_cancellation: asyncutils.exceptions.IgnoreErrors¶
Context manager to ignore
CancelledError.