File size: 463 Bytes
48394b1
 
8a1a65f
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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))