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

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()) #这一条是解决使用windows系统在对https网站发送请求时出现错误,没有网页请求可不加这一条。
start=time.time()
print(start)
asyncio.run(main())
print(time.time()-start)

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

posted @ 2023-05-28 23:20  壮九  阅读(557)  评论(0)    收藏  举报