Async Function Tools
- aiotools.func.apartial(func, /, *args, **kwargs)
Deprecated since version 2.0: Use
functools.partial()directly for async functions as well, since it began to work seamlessly as of Python 3.8.
- lru_cache(maxsize: int | None = 128, typed: bool = False, *, expire_after: float | None = None) Callable[[Callable[[...], Coroutine[Any, Any, _R]] | partial[Coroutine[Any, Any, _R]] | partialmethod[Coroutine[Any, Any, _R]]], _LRUCacheWrapper[_R]][source]
- lru_cache(maxsize: Callable[[...], Coroutine[Any, Any, _R]] | partial[Coroutine[Any, Any, _R]] | partialmethod[Coroutine[Any, Any, _R]], /) _LRUCacheWrapper[_R]
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.
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.,
3and3.0will 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.
Changed in version 2.2.2: This now reuses a more thorough implementation from the aio-libs/async_lru package with a minor difference in the argument naming for backward compatibility with existing aiotools users.
Changed in version 2.2.2: Thanks to the above change, it can now decorate instance methods while preserving the full typing information.