pyfunctools.decorators.async_decorator module

no-index:

pyfunctools.decorators.async_decorator.async_decorator(func: Callable[[Any], Any])[source]

A decorator to run a synchronous function asynchronously using asyncio.

This decorator converts a blocking function into an async function by running it in an executor. It uses the asyncio event loop to handle the execution.

Parameters:

func (callable) – The synchronous function to be decorated.

Returns:

An async function that wraps the original function.

Return type:

callable

Example

>>> import time
>>>
>>> @async_decorator
... def blocking_function(x):
...     import time
...     time.sleep(2)
...     return x * 2
>>>
>>> async def main():
...     result = await blocking_function(5)
...     print(result)
>>>
>>> asyncio.run(main())
10