当前位置:网站首页>asyncio 警告 DeprecationWarning: There is no current event loop
asyncio 警告 DeprecationWarning: There is no current event loop
2022-07-03 10:05:00 【小公鸡卡哇伊呀~】
以下为实现异步抓取网页的代码段:
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))
当前 Python 版本为 3.10.5,运行以上代码,会出现如下警告:
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))
总之 asyncio.get_event_loop(), asyncio.gather(*tasks) 两个函数都不能用。
代码改为使用 asyncio.run() ,测试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())
以下语句必须要加,
# important
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
否则 windows 系统对于 https 网站,会出现错误
RuntimeError: Event loop is closed
边栏推荐
- QT: QSS custom qtableview instance
- 大厂技术专家:工程师如何提升沟通能力?
- 公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
- 【obs】obs的ini格式的ConfigFile
- AMS series - application startup process
- Communication software development and Application
- The normal one inch is 25.4 cm, and the image field is 16 cm
- 栈,单调栈,队列,单调队列
- 项目管理精华读书笔记(六)
- First line of code kotlin notes
猜你喜欢

Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?

面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩

Stack, monotone stack, queue, monotone queue

redis那些事儿

Software testing redis database

What are the strengths of "testers"?

T5 attempt

What kind of living condition is a tester with a monthly salary of more than 10000?

Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure

php服务器 与redis交互大量CLOSE_WAIT分析
随机推荐
ExecutorException: Statement returned more than one row, where no more than one was expected.
Software testing (test case) writing: vulgar, native and skillful
Hal - General
QT: QSS custom qtreeview instance
Typescript learning summary
Tablespace creation management and control file management
Latest sales volume of pinduoduo
公司测试部门来了个00后卷王之王,老油条感叹真干不过,但是...
2022-07-02:以下go语言代码输出什么?A:编译错误;B:Panic;C:NaN。 package main import “fmt“ func mai
IIS does not take effect after modifying the configuration information
Struct function & function pointer
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
Reading notes: heart like Bodhi, Cao Dewang
Stack, monotone stack, queue, monotone queue
After 8 years of industry thinking, the test director has a deeper understanding of test thinking
Solve the problem that pycharm Chinese input method does not follow
行业唯一!法大大电子合同上榜36氪硬核科技企业
How to realize automatic testing in embedded software testing?
Qt:qss custom QSlider instance
CorelDRAW Graphics Suite 2022新版功能详情介绍