当前位置:网站首页>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
边栏推荐
- The element form shows the relationship between elementary transformation and elementary matrix
- Stack, monotone stack, queue, monotone queue
- Empire CMS no thumbnail smart tag (e:loop) two ways to judge whether there is a titlepic
- 面试题总结(2) IO模型,集合,NIO 原理,缓存穿透,击穿雪崩
- What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
- Analysis of JMM memory model
- Communication software development and Application
- IIS does not take effect after modifying the configuration information
- VPP三层网络互联配置
- C语言日志库zlog基本使用
猜你喜欢
How to realize automatic testing in embedded software testing?
T5 attempt
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
可以写进简历的软件测试电商项目,不进来get一下?
php服务器 与redis交互大量CLOSE_WAIT分析
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
Stack, monotone stack, queue, monotone queue
AIDL
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
随机推荐
线性表顺序表综合应用题P18
The normal one inch is 25.4 cm, and the image field is 16 cm
面試題總結(2) IO模型,集合,NIO 原理,緩存穿透,擊穿雪崩
2022 pinduogai 100000 sales tutorial
线性表的双链表
读书笔记:《心若菩提》 曹德旺
File upload and download test point
php服务器 与redis交互大量CLOSE_WAIT分析
Tencent micro app to get wechat user information
Clion debug
I, a tester from a large factory, went to a state-owned enterprise with a 50% pay cut. I regret it
redis那些事儿
大厂技术专家:工程师如何提升沟通能力?
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
Inexplicable problems in the nesting of constraintlayout and relativelayout
Reading notes: heart like Bodhi, Cao Dewang
1. Hal driven development
做软件测试三年,薪资不到20K,今天,我提出了辞职…
2. Hal hardware abstraction layer
C语言日志库zlog基本使用