过早关闭嵌套的异步生成器
Closing a nested async generator prematurely
我正在研究如何在不完全迭代的情况下关闭嵌套的异步生成器。例如:
import asyncio
async def gen1():
yield 1
yield 2
async def gen2():
async for nr in gen1():
yield nr
async def main():
stream = gen2()
print(await stream.__anext__()) # 1
await stream.aclose()
asyncio.run(main())
但是,这会导致 RuntimeError
:
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
最初的问题是在使用 Python 版本 3.7.4
和 3.8.0b4
进行测试时出现的。它已在 3.8.0rc1
:
中修复
我正在研究如何在不完全迭代的情况下关闭嵌套的异步生成器。例如:
import asyncio
async def gen1():
yield 1
yield 2
async def gen2():
async for nr in gen1():
yield nr
async def main():
stream = gen2()
print(await stream.__anext__()) # 1
await stream.aclose()
asyncio.run(main())
但是,这会导致 RuntimeError
:
unhandled exception during asyncio.run() shutdown
task: <Task finished name='Task-2' coro=<<async_generator_athrow without __name__>()> exception=RuntimeError("can't send non-None value to a just-started coroutine")>
RuntimeError: can't send non-None value to a just-started coroutine
最初的问题是在使用 Python 版本 3.7.4
和 3.8.0b4
进行测试时出现的。它已在 3.8.0rc1
: