# 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))