当前位置:网站首页>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
边栏推荐
- 如何成为一名高级数字 IC 设计工程师(1-3)Verilog 编码语法篇:Verilog 行为级、寄存器传输级、门级(抽象级别)
- 2022-07-02: what is the output of the following go language code? A: Compilation error; B:Panic; C:NaN。 package main import “fmt“ func mai
- Tencent micro app to get wechat user information
- 表空间创建管理及控制文件管理
- The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
- The role and necessity of implementing serializable interface
- Google Earth Engine(GEE)——当我们前后影像来弥补插值效果得时候,没有效果怎么办?
- One hot code
- Lecture 1 number field
- Comment réaliser des tests automatisés pour les tests logiciels embarqués?
猜你喜欢

redis那些事儿

UI自动化测试如何走出困境?价值又如何体现?

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

Communication software development and Application

反正切熵(Arctangent entropy):2022.7月最新SCI论文

The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but

LeetCode 46:全排列

Cause: org. apache. ibatis. builder. Builderexception: error parsing SQL mapper configuration problem analysis

Google Earth Engine(GEE)——GHSL 全球人口网格数据集250米分辨率

Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
随机推荐
面试题总结(2) IO模型,集合,NIO 原理,缓存穿透,击穿雪崩
触摸与屏幕自动旋转调试
Error installing the specified version of pilot
QT: QSS custom qtoolbutton instance
POI excel 单元格换行
"Core values of testing" and "super complete learning guide for 0 basic software testing" summarized by test engineers for 8 years
The five-year itch of software testing engineers tells the experience of breaking through bottlenecks for two years
AMS Series 1 - AMS startup process
项目管理精华读书笔记(七)
Kotlin's use of the no Arg compiler plug-in in gradle
Struct function & function pointer
T5 attempt
Execute kubectl on Tencent cloud container service node
QT: QSS custom qtoolbar and qtoolbox instances
【Proteus仿真】74HC154 四线转12线译码器组成的16路流水灯
Encapsulation attempt of network request framework of retro + kotlin + MVVM
ORACLE 11G 单机冷备数据库
Ext file system mechanism principle
Project management essence reading notes (VII)
Overview of testing theory