replyflow-rag / core /executor.py
Qurrah's picture
ADD: enhance documentation with a comment explaining the purpose of the run_blocking function
48394b1
Raw History Blame Contribute Delete
463 Bytes
# runs blocking (synchronous) functions without freezing your async FastAPI server.
import asyncio
from collections.abc import Callable
from concurrent.futures import Executor
from functools import partial
from typing import TypeVar
T = TypeVar("T")
async def run_blocking(executor: Executor, func: Callable[..., T], *args, **kwargs) -> T:
loop = asyncio.get_running_loop()
return await loop.run_in_executor(executor, partial(func, *args, **kwargs))