描述:(简化)
代码:(简体)
import asyncio
async def download(data):
filename = "*" if data in ["b", "c"] else data # simulated failure
with open(filename, "w") as f:
f.write(data)
async def coro(data_list):
coroutines = [download(data) for data in data_list]
for coroutine in asyncio.as_completed(coroutines):
await coroutine
async def main():
task1 = asyncio.create_task(coro(["a", "b", "c"]))
task2 = asyncio.create_task(coro(["d", "e", "f"]))
results = await asyncio.gather(task1, task2, return_exceptions=True)
for _ in results:
pass
asyncio.run(main())
Run Code Online (Sandbox Code Playgroud)
输出:(简化) …