File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -373,6 +373,49 @@ will not be able to add it to its list of active generators because the hooks
373373will be set after the generator tries to call it. Consequently, the event loop
374374will not be able to terminate the generator if necessary.
375375
376+ Consider following example::
377+
378+ import asyncio
379+
380+ async def agenfn():
381+ try:
382+ yield 10
383+ finally:
384+ await asyncio.sleep(0)
385+
386+
387+ with asyncio.Runner() as runner:
388+ agen = agenfn()
389+ print(runner.run(anext(agen)))
390+ del agen
391+
392+ Output::
393+
394+ 10
395+ Exception ignored while closing generator <async_generator object agenfn at 0x000002F71CD10D70>:
396+ Traceback (most recent call last):
397+ File "example.py", line 13, in <module>
398+ del agen
399+ ^^^^
400+ RuntimeError: async generator ignored GeneratorExit
401+
402+ This example can be fixed as follow::
403+
404+ import asyncio
405+
406+ async def agenfn():
407+ try:
408+ yield 10
409+ finally:
410+ await asyncio.sleep(0)
411+
412+ async def main():
413+ agen = agenfn()
414+ print(await anext(agen))
415+ del agen
416+
417+ asyncio.run(main())
418+
376419
377420Avoid iterating and closing the same generator concurrently
378421-----------------------------------------------------------
You can’t perform that action at this time.
0 commit comments