7.17. AsyncIO Iterator

  • __aiter__

  • __anext__

7.17.1. Example

>>> class Reader:
...     async def readline(self):
...         ...
...
...     def __aiter__(self):
...         return self
...
...     async def __anext__(self):
...         line = await self.readline()
...         if line == b'':
...             raise StopAsyncIteration
...         return line

7.17.2. Type Annotation

  • collections.abc.AsyncIterable

  • collections.abc.AsyncIterator

  • collections.abc.AsyncGenerator