Chapter 6. Expressions 70 The Python Language Reference, Release 3.9.5 To take care of finalization, an event loop should define a finalizer function which takes an asynchronous generator- iterator and presumably calls aclose() and executes the coroutine. This finalizer may be registered by calling sys.set_asyncgen_hooks() . When first iterated over, an asynchronous generator-iterator will store the reg- istered finalizer to be called upon finalization. For a reference example of a finalizer method see the implementation of$_{asyncio.Loop.shutdown_asyncgens}$ in Lib/asyncio/base_events.py. The expression$_{yield from <expr>}$ is a syntax error when used in an asynchronous generator function. This subsection describes the methods of an asynchronous generator iterator, which are used to control the execution of a generator function. Asynchronous generator-iterator methods Returns an awaitable which when run starts to execute the asynchronous generator or resumes it at the last executed yield expression. When an asynchronous generator function is resumed with an __anext__() method, the current yield expression always evaluates to$_{None}$ in the returned awaitable, which when run will continue to the next yield expression. The value of the expression_list of the yield expression is the value of the StopIteration exception raised by the completing coroutine. If the asynchronous genera- tor exits without yielding another value, the awaitable instead raises a StopAsyncIteration exception, signalling that the asynchronous iteration has completed. This method is normally called implicitly by a$_{async for}$loop. Returns an awaitable which when run resumes the execution of the asynchronous generator. As with the send() method for a generator, this “sends” a value into the asynchronous generator function, and the value argument becomes the result of the current yield expression. The awaitable returned by the$_{asend()}$ method will return the next value yielded by the generator as the value of the raised$_{StopIteration}$, or raises$_{StopAsyncIteration}$ if the asynchronous generator exits without yielding another value. When asend() is called to start the asynchronous generator, it must be called with$_{None}$ as the argument, because there is no yield expression that could receive the value. Returns an awaitable that raises an exception of type $_{type}$at the point where the asynchronous gener- ator was paused, and returns the next value yielded by the generator function as the value of the raised StopIteration exception. If the asynchronous generator exits without yielding another value, a $_{StopAsyncIteration}$exception is raised by the awaitable. If the generator function does not catch the passed-in exception, or raises a different exception, then when the awaitable is run that exception propagates to the caller of the awaitable. Returns an awaitable that when run will throw a$_{GeneratorExit}$ into the asynchronous generator function at the point where it was paused. If the asynchronous generator function then exits gracefully, is already closed, or raises $_{GeneratorExit}$(by not catching the exception), then the returned awaitable will raise a $_{StopIteration}$exception. Any further awaitables returned by subsequent calls to the asynchronous generator will raise a$_{StopAsyncIteration}$ exception. If the asynchronous generator yields a value, a $_{RuntimeError}$is raised by the awaitable. If the asynchronous generator raises any other exception, it is propagated to the caller of the awaitable. If the asynchronous generator has already exited due to an exception or normal exit, then further calls to$_{aclose()}$ will return an awaitable that does nothing. coroutine agen. aclose () coroutine agen. athrow ( type [, value [, traceback ] ] ) coroutine agen. asend ( value$_{)}$ coroutine agen. __anext__ ()