这是我的代码:
import asyncio
import time
global_list = []
async def wtf(n):
global_list.append(n)
async def main():
task1 = asyncio.create_task(
wtf(200)
)
task2 = asyncio.create_task(
wtf(30)
)
print(f"started at {time.strftime('%X')}")
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
print(global_list)
我运行了10次,全局列表中的内容始终为[200,30]。
鉴于任务是同时运行的,所以我希望内容至少出现几次[30,200]。
我误会了吗?