mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-25 04:57:54 +00:00
Add extra required await
This commit is contained in:
@@ -66,11 +66,15 @@ def cache(
|
|||||||
async def ensure_async_func(*args, **kwargs):
|
async def ensure_async_func(*args, **kwargs):
|
||||||
"""Run cached sync functions in thread pool just like FastAPI."""
|
"""Run cached sync functions in thread pool just like FastAPI."""
|
||||||
if inspect.iscoroutinefunction(func):
|
if inspect.iscoroutinefunction(func):
|
||||||
# async, return as is
|
# async, return as is.
|
||||||
return func(*args, **kwargs)
|
# 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:
|
else:
|
||||||
# sync, wrap in thread and return async
|
# 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()
|
copy_kwargs = kwargs.copy()
|
||||||
|
|||||||
Reference in New Issue
Block a user