假设以下代码:
import asyncio
import time
async def say_after():
for i in range(10):
await asyncio.sleep(1)
print("first function")
async def say_after2():
for i in range(10):
await asyncio.sleep(1)
print("second function")
async def main():
await say_after()
await say_after2()
asyncio.run(main())
它将首先打印"first function"
10次,并在功能say_after
完成后再打印"second function"
10次。现在我想同时运行两个函数,例如2个线程(例如并行),而asycio
不能按顺序执行。我该怎么办?