Take a blocking function and create an async one that receives the same positional and keyword arguments. Usage: ```python def blocking_func(arg1, arg2, kwarg1=None): # blocking code return result result = asyncify(blocking_function)(arg1, arg2, kwarg1=va
(function: Callable[T_ParamSpec, T_Retval])
| 26 | |
| 27 | # inspired by `asyncer`, https://github.com/tiangolo/asyncer |
| 28 | def asyncify(function: Callable[T_ParamSpec, T_Retval]) -> Callable[T_ParamSpec, Awaitable[T_Retval]]: |
| 29 | """ |
| 30 | Take a blocking function and create an async one that receives the same |
| 31 | positional and keyword arguments. |
| 32 | |
| 33 | Usage: |
| 34 | |
| 35 | ```python |
| 36 | def blocking_func(arg1, arg2, kwarg1=None): |
| 37 | # blocking code |
| 38 | return result |
| 39 | |
| 40 | |
| 41 | result = asyncify(blocking_function)(arg1, arg2, kwarg1=value1) |
| 42 | ``` |
| 43 | |
| 44 | ## Arguments |
| 45 | |
| 46 | `function`: a blocking regular callable (e.g. a function) |
| 47 | |
| 48 | ## Return |
| 49 | |
| 50 | An async function that takes the same positional and keyword arguments as the |
| 51 | original one, that when called runs the same original function in a thread worker |
| 52 | and returns the result. |
| 53 | """ |
| 54 | |
| 55 | async def wrapper(*args: T_ParamSpec.args, **kwargs: T_ParamSpec.kwargs) -> T_Retval: |
| 56 | return await to_thread(function, *args, **kwargs) |
| 57 | |
| 58 | return wrapper |
no outgoing calls