当前位置:网站首页>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
边栏推荐
- 如何成为一名高级数字 IC 设计工程师(1-4)Verilog 编码语法篇:表达式
- ORACLE 11G 单机冷备数据库
- 触摸与屏幕自动旋转调试
- VPP三层网络互联配置
- CorelDRAW Graphics Suite 2022新版功能详情介绍
- How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
- 高精度室内定位技术,在智慧工厂安全管理的应用
- 帝国cms 无缩略图 灵动标签(e:loop)判断有无标题图片(titlepic)的两种写法
- 如何成为一名高级数字 IC 设计工程师(1-2)Verilog 编码语法篇:Verilog 1995、2001、2005 标准
- 如何让让别人畏惧你
猜你喜欢
10. Nacos source code construction
Arctangent entropy: the latest SCI paper in July 2022
Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
英特尔13代酷睿旗舰曝光,单核5.5GHz
Leetcode 46: full arrangement
Processes and threads
解决undefined reference to `__aeabi_uidivmod‘和undefined reference to `__aeabi_uidiv‘错误
My understanding of testing (summarized by senior testers)
LeetCode 46:全排列
Probability theory: application of convolution in calculating moving average
随机推荐
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards
多维度监控:智能监控的数据基础
php服务器 与redis交互大量CLOSE_WAIT分析
Internet socket (non) blocking write/read n bytes
Summary of interview questions (2) IO model, set, NiO principle, cache penetration, breakdown avalanche
线性表顺序表综合应用题P18
[vtk] source code interpretation of vtkpolydatatoimagestencil
动态规划(区间dp)
one hot 独热码
ASP. Net hotel management system
Bi skills - permission axis
1. Hal driven development
Reading notes: heart like Bodhi, Cao Dewang
基于I2C协议的驱动开发
如何成为一名高级数字 IC 设计工程师(1-2)Verilog 编码语法篇:Verilog 1995、2001、2005 标准
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
C语言 AES加解密
ASP.NET-酒店管理系统
11. Provider service registration of Nacos service registration source code analysis
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests