nemo_microservices._utils._sync#

Module Contents#

Functions#

asyncify

Take a blocking function and create an async one that receives the same positional and keyword arguments. For python version 3.9 and above, it uses asyncio.to_thread to run the function in a separate thread. For python version 3.8, it uses locally defined copy of the asyncio.to_thread function which was introduced in python 3.9.

to_thread

Data#

API#

nemo_microservices._utils._sync.T_ParamSpec#

‘ParamSpec(…)’

nemo_microservices._utils._sync.T_Retval#

‘TypeVar(…)’

nemo_microservices._utils._sync.asyncify(
function: Callable[nemo_microservices._utils._sync.T_ParamSpec, nemo_microservices._utils._sync.T_Retval],
) Callable[nemo_microservices._utils._sync.T_ParamSpec, Awaitable[nemo_microservices._utils._sync.T_Retval]]#

Take a blocking function and create an async one that receives the same positional and keyword arguments. For python version 3.9 and above, it uses asyncio.to_thread to run the function in a separate thread. For python version 3.8, it uses locally defined copy of the asyncio.to_thread function which was introduced in python 3.9.

Usage:

def blocking_func(arg1, arg2, kwarg1=None):
    # blocking code
    return result


result = asyncify(blocking_function)(arg1, arg2, kwarg1=value1)

Arguments

function: a blocking regular callable (e.g. a function)

Return

An async function that takes the same positional and keyword arguments as the original one, that when called runs the same original function in a thread worker and returns the result.

async nemo_microservices._utils._sync.to_thread(
func: Callable[nemo_microservices._utils._sync.T_ParamSpec, nemo_microservices._utils._sync.T_Retval],
/,
*args: nemo_microservices._utils._sync.T_ParamSpec,
**kwargs: nemo_microservices._utils._sync.T_ParamSpec,
) nemo_microservices._utils._sync.T_Retval#