Asynchronous Iterators and Iterables in Python
When you write asynchronous code in Python, you’ll likely need to create asynchronous iterators and iterables at some point. Asynchronous iterators are what Python uses to control async for
loops, while asynchronous iterables are objects that you can iterate over using async for
loops.
Both tools allow you to iterate over awaitable objects without blocking your code. This way, you can perform different tasks asynchronously.
In this tutorial, you’ll:
- Learn what async iterators and iterables are in Python
- Create async generator expressions and generator iterators
- Code async iterators and iterables with the
.__aiter__()
and.__anext__()
methods - Use async iterators in async loops and comprehensions
To get the most out of this tutorial, you should know the basics of Python’s