The Python Language Reference, Release 3.9.5 3.4.2 Coroutine Objects Coroutine objects are awaitable objects. A coroutine’s execution can be controlled by calling$_{__await__()}$ and iterating over the result. When the coroutine has finished executing and returns, the iterator raises$_{StopIteration}$, and the exception’s value attribute holds the return value. If the coroutine raises an exception, it is propagated by the iterator. Coroutines should not directly raise unhandled StopIteration exceptions. Coroutines also have the methods listed below, which are analogous to those of generators (see Generator-iterator methods ). However, unlike generators, coroutines do not directly support iteration. Changed in version 3.5.2: It is a RuntimeError to await on a coroutine more than once. Starts or resumes execution of the coroutine. If value is None , this is equivalent to advancing the iterator returned by __await__() . If value is not None , this method delegates to the send() method of the iterator that caused the coroutine to suspend. The result (return value,$_{StopIteration}$, or other exception) is the same as when iterating over the$_{__await__()}$ return value, described above. Raises the specified exception in the coroutine. This method delegates to the$_{throw()}$ method of the iterator that caused the coroutine to suspend, if it has such a method. Otherwise, the exception is raised at the suspension point. The result (return value, StopIteration , or other exception) is the same as when iterating over the __await__() return value, described above. If the exception is not caught in the coroutine, it propagates back to the caller. Causes the coroutine to clean itself up and exit. If the coroutine is suspended, this method first delegates to the close() method of the iterator that caused the coroutine to suspend, if it has such a method. Then it raises $_{GeneratorExit}$at the suspension point, causing the coroutine to immediately clean itself up. Finally, the coroutine is marked as having finished executing, even if it was never started. Coroutine objects are automatically closed using the above process when they are about to be destroyed. An asynchronous iterator can call asynchronous code in its$_{__anext__}$ method. Asynchronous iterators can be used in an$_{async for}$statement. Must return an asynchronous iterator object. Must return an awaitable resulting in a next value of the iterator. Should raise a$_{StopAsyncIteration}$ error when the iteration is over. An example of an asynchronous iterable object: coroutine. send ( value$_{)}$ coroutine. throw ( type [, value [, traceback ] ] ) coroutine. close () object. __aiter__ ( self$_{)}$ object. __anext__ ( self$_{)}$ classReader : async def readline(self): ... def __aiter__ (self): return self async def __anext__ (self): val= await self.readline() if val==b'': raise StopAsyncIteration return val 3.4.3 Asynchronous Iterators 3.4. Coroutines 43 New in version 3.5.