Message396569
In the following example:
def gen():
try:
yield 1
finally:
print('finalize inner')
def func():
try:
for x in gen():
break
finally:
print('finalize outer')
func()
print('END')
the output in CPython is:
finalize inner
finalize outer
END
But in similar example for asynchronous generator:
async def gen():
try:
yield 1
finally:
print('finalize inner')
async def func():
try:
async for x in gen():
break
finally:
print('finalize outer')
import asyncio
asyncio.run(func())
print('END')
the output in CPython is:
finalize outer
finalize inner
END
There is a strong link somewhere which prevents finalization of the asynchronous generator object until leaving the outer function.
Tested on CPython 3.7-3.11. In PyPy "finalize inner" is not printed at all. Using closing() and aclosing() is the right way to get deterministic finalization, but in any case it would be better to get rid of strong link which prevents finalization of the asynchronous generator object. |
|
Date |
User |
Action |
Args |
2021-06-27 09:11:32 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, asvetlov, Mark.Shannon, yselivanov |
2021-06-27 09:11:32 | serhiy.storchaka | set | messageid: <1624785092.65.0.844977946345.issue44518@roundup.psfhosted.org> |
2021-06-27 09:11:32 | serhiy.storchaka | link | issue44518 messages |
2021-06-27 09:11:32 | serhiy.storchaka | create | |
|