@@ -253,23 +253,24 @@ Output in debug mode::
253253Asynchronous generators best practices
254254======================================
255255
256- By :term: ` asynchronous generator ` in this section we will mean
257- an :term: ` asynchronous generator iterator ` that is returned by an
258- :term: ` asynchronous generator ` function .
256+ Writing correct and efficient asyncio code requires avoiding some known gotchas.
257+ This section outlines the best practices you need to know.
258+ Following these practices will save you hours of debugging .
259259
260260
261261Manually close the generator
262262----------------------------
263263
264- If an asynchronous generator happens to exit early by :keyword: `break `, the caller
265- task being cancelled, or other exceptions, the generator's async cleanup code
266- will run in an unexpected context -- perhaps after the lifetime of tasks it depends on, or
267- during the event loop shutdown when the async-generator garbage collection hook
268- is called.
264+ It is recommended to manually close the
265+ :term: `asynchronous generator <asynchronous generator iterator> `. If the generator
266+ exits early by exception raised in the for-loop body, for example,
267+ the generator's async cleanup code will run in an unexpected context. This could
268+ happen after the lifetime of tasks it depends on, or during the event loop
269+ shutdown when the async-generator garbage collection hook is called.
269270
270271To prevent this, it is recommended to explicitly close the async generator by
271- calling the :meth: `~agen.aclose ` method, or using a :func: `contextlib.aclosing ` context
272- manager::
272+ calling the :meth: `~agen.aclose ` method, or by using a :func: `contextlib.aclosing `
273+ context manager::
273274
274275 import asyncio
275276 import contextlib
@@ -289,8 +290,9 @@ manager::
289290Only create a generator when a loop is already running
290291------------------------------------------------------
291292
292- It is recommended to create asynchronous generators only after the event loop
293- has already been created.
293+ It is recommended to create
294+ :term: `asynchronous generators <asynchronous generator iterator> ` only after
295+ the event loop has already been created.
294296
295297To ensure that asynchronous generators close reliably, the event loop uses the
296298:func: `sys.set_asyncgen_hooks ` function to register callback functions. These
0 commit comments