Async Function Tools
- apartial(coro, *args, **kwargs)[source]
Wraps a coroutine function with pre-defined arguments (including keyword arguments). It is an asynchronous version of
functools.partial()
.
- lru_cache(maxsize: int = 128, typed: bool = False, expire_after: Optional[float] = None)[source]
A simple LRU cache just like
functools.lru_cache()
, but it works for coroutines. This is not as heavily optimized asfunctools.lru_cache()
which uses an internal C implementation, as it targets async operations that take a long time.It follows the same API that the standard functools provides. The wrapped function has
cache_clear()
method to flush the cache manually, but leavescache_info()
for statistics unimplemented.Note that calling the coroutine multiple times with the same arguments before the first call returns may incur duplicate executions.
This function is not thread-safe.
- Parameters
maxsize – The maximum number of cached entries.
typed – Cache keys in different types separately (e.g.,
3
and3.0
will be different keys).expire_after – Re-calculate the value if the configured time has passed even when the cache is hit. When re-calculation happens the expiration timer is also reset.