asyncify
⚓︎
Asyncify
Modules:
-
aios–aios = asyncio + os
-
core–Asyncify core
Functions:
-
aiorun_anyio–Run an async function or awaitable using anyio
-
aiterable–Convert any-iterable to an async iterator
-
asyncify–Makes a sync function async
-
await_or_return–Return the result of an awaitable or return the object
-
is_async–Return True if function/object is async/awaitable
-
run–Run an async/awaitable function (Polyfill asyncio.run)
aiorun_anyio
⚓︎
aiorun_anyio(
awaitable_or_func: Awaitable[T_Retval]
| Callable[..., Coroutine[Any, Any, T_Retval]],
*args: object,
backend: str = "asyncio",
backend_options: dict[str, Any] | None = None,
) -> T_Retval
Run an async function or awaitable using anyio
Parameters:
-
(awaitable_or_func⚓︎Awaitable[T_Retval] | Callable[..., Coroutine[Any, Any, T_Retval]]) –Function or awaitable to run
-
(*args⚓︎object, default:()) –args to pass to the function
-
(backend⚓︎str, default:'asyncio') –Backend to use for running the function
-
(backend_options⚓︎dict[str, Any] | None, default:None) –Options to pass to the backend
Returns:
-
T_Retval(T_Retval) –Return value of the function
aiterable
⚓︎
aiterable(
it: Iterable[T] | AsyncIterable[T],
) -> AsyncIterator[T]
Convert any-iterable to an async iterator
Examples:
>>> from os import remove
>>> from asyncio import run
>>> plain_jane_list = list(range(10))
>>> async def consume_aiterable(it):
... stuff = []
... async for el in aiterable(it):
... stuff.append(el)
... return stuff
>>> run(consume_aiterable(plain_jane_list))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> async def async_gen():
... for b in range(10):
... yield b
>>> run(consume_aiterable(async_gen()))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> class AsyncIterable:
... def __aiter__(self):
... return async_gen()
>>> run(consume_aiterable(AsyncIterable()))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
asyncify
⚓︎
asyncify(
funk: Callable[P, T],
*,
loop: AbstractEventLoop | None = None,
executor: Any | None = None,
) -> Callable[P, Awaitable[T]]
Makes a sync function async
Parameters:
-
(funk⚓︎Callable[P, T]) –Function to make into an async coroutine
-
(loop⚓︎AbstractEventLoop | None, default:None) –Event loop in which to run/execute
-
(executor⚓︎Any | None, default:None) –Executor to with which to execute
Returns:
Examples:
>>> from asyncify import asyncify
>>> def add(a, b):
... return a + b
>>> add(1, 5)
6
>>> @asyncify
... def add_async(a, b):
... return a + b
>>> from asyncio import run
>>> run(add_async(1, 5))
6
await_or_return
async
⚓︎
await_or_return(obj: Awaitable[T] | T) -> T
Return the result of an awaitable or return the object
Examples:
>>> from asyncify import await_or_return, run
>>> def add(a, b): return a + b
>>> run(await_or_return(add(1, 4)))
5
>>> async def add_(a, b): return a + b
>>> run(await_or_return(add_(1, 4)))
5
is_async
⚓︎
Return True if function/object is async/awaitable
Parameters:
Returns:
-
bool(bool) –True if the object is async/awaitable; False otherwise
Examples:
>>> from asyncify import is_async
>>> def add(a, b): return a + b
>>> is_async(add)
False
>>> async def add_(a, b): return a + b
>>> is_async(add_)
True
run
⚓︎
Run an async/awaitable function (Polyfill asyncio.run)
Emulate asyncio.run() for snakes below python 3.7; asyncio.run was
added in python3.7.
Parameters:
-
(aw⚓︎Awaitable[T]) –Async/awaitable function to run
-
(debug⚓︎Optional[bool], default:None) –If True run event loop in debug mode
-
(**kwargs⚓︎Any, default:{}) –keyword arguments to be passed to the wrapped function
Returns:
-
T(T) –Return the result of running the async function
Examples:
>>> async def add(a, b):
... return a + b
...
>>> from asyncify.core import run
>>> run(add(1, 4))
5