Asynchronous Iteration and Asynchronous Generators

Learn about asynchronous iteration and asynchronous generators in Python.

In the same way that we have the iterator objects (that is, objects that support being iterated over with Python's built-in for loop), we can do the same, but in an asynchronous fashion.

The __anext__ and __aiter__ coroutines

Imagine we want to create an iterator to abstract the way in which we read data from an external source (like a database), but the part that extracts the data itself is a coroutine, so we couldn't call it during the already familiar __next__ operation as before. That's why we need to make use of the __anext__ coroutine.

The following example illustrates in a simple way how this can be achieved. Disregarding external dependencies, or any other accidental complexity, we'll focus on the methods that make this type of operation possible in order to study them:

Get hands-on with 1200+ tech skills courses.