python 使用 asyncio 执行并发任务
import asyncio
async def factorial(name, number):
print(f"开始执行 {name}")
print(f"Task {name}: factorial({number})")
await asyncio.sleep(3)
print(f"任务执行结束{name}")
return number
async def main():
task = []
name_list = [("A", 2), ("B", 3), ("C", 4)]
for name in name_list:
task.append(factorial(name[0], name[1]))
L = await asyncio.gather(
*task
)
print(L)
asyncio.run(main())