From d123ec4bfa8de039325c24adbb6b64d150cd750c Mon Sep 17 00:00:00 2001 From: "Charl P. Botha" Date: Fri, 14 Oct 2022 14:09:02 +0200 Subject: [PATCH] Add extra required await --- fastapi_cache/decorator.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fastapi_cache/decorator.py b/fastapi_cache/decorator.py index d24c565..b203bb3 100644 --- a/fastapi_cache/decorator.py +++ b/fastapi_cache/decorator.py @@ -66,11 +66,15 @@ def cache( async def ensure_async_func(*args, **kwargs): """Run cached sync functions in thread pool just like FastAPI.""" if inspect.iscoroutinefunction(func): - # async, return as is - return func(*args, **kwargs) + # async, return as is. + # unintuitively, we have to await once here, so that caller + # does not have to await twice. See + # https://stackoverflow.com/a/59268198/532513 + return await func(*args, **kwargs) else: # sync, wrap in thread and return async - return run_in_threadpool(func, *args, **kwargs) + # see above why we have to await even although caller also awaits. + return await run_in_threadpool(func, *args, **kwargs) copy_kwargs = kwargs.copy()