Add extra required await

This commit is contained in:
Charl P. Botha
2022-10-14 14:09:02 +02:00
parent eeea884bb4
commit d123ec4bfa

View File

@@ -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()