asyncstdlib library re-implements functions and classes of the Python
standard library to make them compatible with
async callables, iterables
and context managers.
It is fully agnostic to
async event loops and seamlessly works with
asyncio, third-party libraries such as
trio, as well as
async event loop.
Standard Library Modules¶
All re-implementations are located in submodules of
with the same name as those of the Python standard library.
Replicates any Built-in Functions that benefit from being asynchronous, such as
functoolsthat benefit from being asynchronous, which is just
contextlibtools that benefit from being asynchronous, such as
itertoolsthat benefit from being asynchronous, such as
heapqtools that benefit from being asynchronous, which is just
For simplicity, the
asyncstdlib namespace also exposes all individual
functions and classes directly.
asyncstdlib.builtins.enumerate is also available
The Async Library Module¶
The core toolset used by
asyncstdlib itself is available
as a separate submodule.
asyncstdlibtools useful for building well-behaved
asynchelpers and programs.
Async Neutral Arguments¶
Many objects of
asyncstdlib are async neutral – they accept
both regular and async arguments.
Type annotations use (async) to denote async neutral objects.
For example, the annotation (int, …) → (async) bool denotes a call that takes an
int and either returns a boolean directly or requires
return a boolean.
Whether a call is regular or async is determined by inspecting its
return type at runtime.
This supports async-producing factories, such as an
function wrapped in
However, this also means that the result must consistently be either
regular or async.
Note that only arguments to
asyncstdlib may be async neutral.
All callables of
asyncstdlib consistently provide
asynchronous iterators, and
asynchronous context managers.
Async Iterator Cleanup¶
Cleanup of async iterables is special in that
aclose() may require
an active event loop. Thus, all utilities of
asyncstdlib that work on async
iterators will eagerly
borrow() to prevent automatic cleanup,
scoped_iter() to guarantee cleanup in custom code.
See the guide on Iterator Scoping for details and usage examples.