mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47: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):
|
||||
"""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()
|
||||
|
||||
Reference in New Issue
Block a user