From f310ef5b2d00c7065b99ae4c5a0a563ac1c631b0 Mon Sep 17 00:00:00 2001 From: Yurii Karabas <1998uriyyo@gmail.com> Date: Fri, 9 Sep 2022 19:35:48 +0300 Subject: [PATCH] Use run_in_threadpool instead of asyncio run_in_executor --- fastapi_cache/decorator.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index ed1a30c..5bd88b2 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -1,21 +1,18 @@ -import asyncio -from functools import wraps, partial import inspect -from typing import TYPE_CHECKING, Callable, Optional, Type +from functools import wraps +from typing import Callable, Optional, Type + +from fastapi.concurrency import run_in_threadpool from fastapi_cache import FastAPICache from fastapi_cache.coder import Coder -if TYPE_CHECKING: - import concurrent.futures - def cache( expire: int = None, coder: Type[Coder] = None, key_builder: Callable = None, namespace: Optional[str] = "", - executor: Optional["concurrent.futures.Executor"] = None, ): """ cache all function @@ -23,7 +20,6 @@ def cache( :param expire: :param coder: :param key_builder: - :param executor: :return: """ @@ -74,8 +70,7 @@ def cache( if inspect.iscoroutinefunction(func): ret = await func(*args, **kwargs) else: - loop = asyncio.get_event_loop() - ret = await loop.run_in_executor(executor, partial(func, *args, **kwargs)) + ret = await run_in_threadpool(func, *args, **kwargs) await backend.set(cache_key, coder.encode(ret), expire or FastAPICache.get_expire()) return ret