python3.10版本以后使用asyncio不报错方法

发布时间 2023-05-28 23:24:32作者: 壮九
import asyncio
import time


async def func1():
    print(1)
    await asyncio.sleep(2)
    print(2)

async def func2():
    print(3)
    await asyncio.sleep(2)
    print(4)

async def main():
    task=[
        asyncio.ensure_future(func1()),
        asyncio.ensure_future(func2()),
        ]
    await asyncio.gather(*task)

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
start=time.time()
print(start)
asyncio.run(main())
print(time.time()-start)

 3.10版本使用asyncio时会报这个错误/警告:DeprecationWarning: There is no current event loop,使用上述代码可解决这个问题