当前位置:网站首页>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
边栏推荐
- [proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
- Overview of testing theory
- 2021 postgraduate entrance examination mathematics 2 linear algebra
- Using activity to realize a simple inputable dialog box
- Matlab memory variable management command
- Tencent micro app to get wechat user information
- 进程与线程
- 浅析-JMM内存模型
- Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
- Hard goods | write all the codes as soon as you change the test steps? Why not try yaml to realize data-driven?
猜你喜欢
Communication software development and Application
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
Differences among norm, normalize and normalized in eigen
EPS电动转向系统分析
反正切熵(Arctangent entropy):2022.7月最新SCI论文
[true question of the Blue Bridge Cup trials 44] scratch eliminate the skeleton Legion children programming explanation of the true question of the Blue Bridge Cup trials
A simple method of adding dividing lines in recyclerview
Tencent micro app to get wechat user information
What are the strengths of "testers"?
Probability theory: application of convolution in calculating moving average
随机推荐
在职美团测试工程师的这八年,我是如何成长的,愿技术人看完都有收获
10. Nacos source code construction
触摸与屏幕自动旋转调试
英特尔13代酷睿旗舰曝光,单核5.5GHz
如何让让别人畏惧你
Crawl with requests
Touch and screen automatic rotation debugging
Typescript learning summary
Function details of CorelDRAW graphics suite 2022
EPS电动转向系统分析
读书笔记:《心若菩提》 曹德旺
What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
2022 pinduogai 100000 sales tutorial
Is pinduogai's sales safe in 2022?
QT: QSS custom qtreeview instance
File upload and download test point
QT: QSS custom qsplitter instance
Differences among norm, normalize and normalized in eigen