为什么像下面的示例一样包装异步生成器时 asyncio.timeout 不起作用?async def generate_many_numbers(): for i in range(1000000000): Yield iasync def main2(): async
为什么像下面的例子一样包装异步生成器时 asyncio.timeout 不起作用?
async def generate_many_numbers():
for i in range(1000000000):
yield i
async def main2():
async with asyncio.timeout(1):
async for number in generate_many_numbers():
print(number)
asyncio.run(main2())
async def generate_many_numbers():
for i in range(1000000000):
await asyncio.sleep(0)
yield i
async def iterate_through_numbers():
async for number in generate_many_numbers():
print(number)
async def main2():
async with asyncio.timeout(1):
async for number in generate_many_numbers():
print(number)
async def main3():
try:
await asyncio.wait_for(iterate_through_numbers(), timeout=1)
except asyncio.TimeoutError:
print("The task timed out!")