当前位置:网站首页>Asyncio warning deprecationwarning: there is no current event loop
Asyncio warning deprecationwarning: there is no current event loop
2022-07-03 11:24:00 【Little cock Kawaii~】
The following is a code snippet for asynchronous web page fetching :
import asyncio
import aiohttp
async def fetch_page(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return response.status
tasks = [fetch_page('http://books.toscrape.com') for i in range(50)]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks))
At present Python Version is 3.10.5, Run the above code , The following warning will appear :
DeprecationWarning: There is no current event loop
loop = asyncio.get_event_loop()
DeprecationWarning: There is no current event loop
loop.run_until_complete(asyncio.gather(*tasks))
All in all asyncio.get_event_loop(), asyncio.gather(*tasks) Neither function can be used .
Code changed to use asyncio.run() , test ok:
import asyncio
import aiohttp
async def fetch_page(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
print(response.status)
async def main():
tasks = [fetch_page('https://quotes.toscrape.com/') for i in range(50)]
await asyncio.gather(*tasks) # argument unpacking: (tasks[0], tasks[1], ... tasks[49])
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
The following statement must be added ,
# important
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
otherwise windows System for https Website , There will be errors
RuntimeError: Event loop is closed
边栏推荐
- 有赞CTO崔玉松:有赞Jarvis核心目标是使产品变得更加聪明和可靠
- Technical experts from large factories: how can engineers improve their communication skills?
- C语言日志库zlog基本使用
- Project management essence reading notes (6)
- Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
- Encapsulation attempt of network request framework of retro + kotlin + MVVM
- 软考中级软件设计师该怎么备考
- 在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
- 【Proteus仿真】74HC154 四线转12线译码器组成的16路流水灯
- [vtk] source code interpretation of vtkpolydatatoimagestencil
猜你喜欢

Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests

I have been doing software testing for three years, and my salary is less than 20K. Today, I put forward my resignation

My understanding of testing (summarized by senior testers)

软考中级软件设计师该怎么备考

A simple method of adding dividing lines in recyclerview

活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点

ASP. Net hotel management system

"Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years

ASP.NET-酒店管理系统

Application of high-precision indoor positioning technology in safety management of smart factory
随机推荐
ASP.NET-酒店管理系统
C language log base zlog basic use
How can UI automated testing get out of trouble? How to embody the value?
EPS电动转向系统分析
2. Hal hardware abstraction layer
CorelDRAW Graphics Suite 2022新版功能详情介绍
Incremental database backup - DB incr DB full
Solution: jupyter notebook does not pop up the default browser
Oracle withdraw permission & create role
行业唯一!法大大电子合同上榜36氪硬核科技企业
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
Unique in the industry! Fada electronic contract is on the list of 36 krypton hard core technology enterprises
在腾讯云容器服务Node上执行 kubectl
基于I2C协议的驱动开发
【obs】obs的ini格式的ConfigFile
Analysis of JMM memory model
Google Earth engine (GEE) -- when we use the front and back images to make up for the interpolation effect, what if there is no effect?
Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
LeetCode 46:全排列
软考中级软件设计师该怎么备考