- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的代码如下:
import asyncio
import aiohttp
urls = [
'http://www.163.com/',
'http://www.sina.com.cn/',
'https://www.hupu.com/',
'http://www.csdn.net/'
]
async def get_url_data(u):
"""
read url data
:param u:
:return:
"""
print('running ', u)
resp = await aiohttp.ClientSession().get(url=u)
headers = resp.headers
print(u, headers)
return headers
async def request_url(u):
"""
main func
:param u:
:return:
"""
res = await get_url_data(u)
return res
loop = asyncio.get_event_loop()
task_lists = asyncio.wait([request_url(u) for u in urls])
loop.run_until_complete(task_lists)
loop.close()
当我运行我的代码时,它会显示一条警告消息:未关闭的客户 session
谁能给我一些解决方案?
非常感谢
最佳答案
最后你应该关闭连接。您有 2 个选择:
您可以手动关闭连接:
import aiohttp
session = aiohttp.ClientSession()
# use the session here
session.close()
或者您可以将它与上下文管理器一起使用:
import aiohttp
import asyncio
async def fetch(client):
async with client.get('http://python.org') as resp:
assert resp.status == 200
return await resp.text()
async def main(loop):
async with aiohttp.ClientSession(loop=loop) as client:
html = await fetch(client)
print(html)
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
The client session supports the context manager protocol for self closing.
关于Python包-aiohttp有警告信息 "Unclosed client session",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46112848/
if(1
if(1
public class Constants { public enum Status{ sen(3,"发送中"), suc(8,"成功"),
我需要匹配以下日期模式: dd-mmm-yyyy (TBC) dd-mmm-yyyy (TBD) dd-mmm-yyyy 但我收到以下异常,但我不明白错误是什么。我非常确定日期部分,我怀疑错误来源是
我正在尝试运行 sql 命令: SELECT *FROM wp_options WHERE option_name = 'active_plugins'; 但是我只收到一个错误,指出有一个未闭合的引号
我有一个用 Python 编写的 Reddit 机器人,有时我会收到以下错误: sys:1: ResourceWarning: unclosed ssl.SSLSocket fd=4, family=
我正在尝试实现此正则表达式来验证电子邮件: ([-!#-'*+/-9=?A-Z^-~]+(\.[-!#-'*+/-9=?A-Z^-~]+)* |"([]!#-[^-~\t]|(\\[\t -~]))+
将模板从 Java 转换为 Scala 时,我注意到以下带有多行注释的怪癖可以简化为以下代码段: /** * /* */ class Blah {} 上面的代码无法编译并显示“错误:未关闭的注释”
以下抛出异常: Pattern.matches(""+input.charAt(i),"\\s"); java.util.regex.PatternSyntaxException: Unclosed
我正在使用在 How to validate an email 找到的电子邮件验证模式它工作正常,除了它允许在电子邮件的第一部分使用 + 而这在我的规范中是不允许的。原代码为 public stati
查询 SELECT * FROM user WHERE username = 'Omnion' AND disable_flag = '0' 此查询在本地主机上运行良好, 上传到服务器后出现问题 Er
我的问题很简单:如果 html 内容中有像这个 img 标签这样的标签,有没有办法将 java 中的 html 解析为 DOM 文档? 这是在解析这些元素时给我一个 SAXException 的代码
我的代码如下: import asyncio import aiohttp urls = [ 'http://www.163.com/', 'http://www.sina.com.c
我正在移植 library到 Python3。弹出的问题之一是所有 "unclosed file" warnings运行测试套件时。我已经解决了 95% 的警告,但还有一些警告仍然存在,我正在努力寻找
最近,我在 Visual Studio 代码上编写 React 应用程序时遇到了问题。由于这个问题,每当我在 React 组件的渲染函数中编写 JSX 并保存它时,它就会变得困惑(我的意思是缩进会变得
在学习网络容器类(class)时Network Containers Lesson我遇到了一个错误 Template parsing error: template: :1: unclosed act
当我使用 ant 编译 Web 应用程序时,我收到以下编译器消息: unclosed character literal 有问题的代码行是: protected char[] diacriticVow
我正在尝试用 java 编写 emailValidator 函数,但遇到问题: public static boolean EmailValidator(String mail) { Patt
我使用 Hive SQL 使用 spark 执行此查询: var hiveContext = new org.apache.spark.sql.hive.HiveContext(sc) result
我正在开发 cv 并使用了 Realm 数据库,但出现以下错误 public class KitabSawti extends RealmObject { ^ warning: Unclosed fi
我是一名优秀的程序员,十分优秀!