Python aiohttp运行时错误:等待时间不早';不用于将来
我知道,但是答案并没有说明这里发生了什么 使用Python aiohttp运行时错误:等待时间不早';不用于将来,python,python-asyncio,aiohttp,Python,Python Asyncio,Aiohttp,我知道,但是答案并没有说明这里发生了什么 使用aiohttp时,我遇到了以下错误: File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect return await fut RuntimeError: await wasn't used with future Unclos
aiohttp
时,我遇到了以下错误:
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
return await fut
RuntimeError: await wasn't used with future
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fb28040f760>
看起来我正在正确地等待事件循环,但我一直在关注这个问题,这会带来什么
版本:
aiohttp==3.7.4.post0
asyncio==3.4.3
Python 3.8.0
这里可能发生两件事:
url
很可能达到了这个阈值,您将被踢出。更改行:
tasks = [session.get(url) for url in urls]
到
我知道这并不能直接回答您的问题,但实际上在
aiohttp
中有一些异步调用,因此您必须在几个地方等待
:
import asyncio
import aiohttp
URL = "https://stackoverflow.com"
async def main():
session = aiohttp.ClientSession()
resp = await session.get(URL)
html = await resp.text()
print(f"{URL} <{resp.status}> ({len(html)})")
resp.release()
await session.close()
if __name__ == "__main__":
asyncio.run(main())
导入异步IO
进口aiohttp
URL=”https://stackoverflow.com"
异步def main():
session=aiohttp.ClientSession()
resp=wait session.get(URL)
html=wait resp.text()
打印(f“{URL}({len(html)})”)
分别发布()
等待会话。关闭()
如果名称=“\uuuuu main\uuuuuuuu”:
asyncio.run(main())
通常更简单的方法是使用该方法,以确保正确关闭所有剩余资源:
import asyncio
import aiohttp
URLS = [
"https://serverfault.com",
"https://stackoverflow.com",
]
async def fetch(session, url):
async with session.get(url) as resp:
html = await resp.text()
print(f"{url} <{resp.status}>")
return {url: len(html)}
async def main():
tasks = []
async with aiohttp.ClientSession() as session:
for url in URLS:
tasks.append(asyncio.create_task(fetch(session, url)))
data = await asyncio.gather(*tasks)
print(data)
if __name__ == "__main__":
asyncio.run(main())
导入异步IO
进口aiohttp
URL=[
"https://serverfault.com",
"https://stackoverflow.com",
]
异步def提取(会话,url):
与session.get(url)作为resp异步:
html=wait resp.text()
打印(f“{url}”)
返回{url:len(html)}
异步def main():
任务=[]
与aiohttp.ClientSession()作为会话异步:
对于url中的url:
tasks.append(asyncio.create_任务(获取(会话,url)))
数据=等待asyncio.gather(*任务)
打印(数据)
如果名称=“\uuuuu main\uuuuuuuu”:
asyncio.run(main())
测试:
$ python test.py
https://serverfault.com <200>
https://stackoverflow.com <200>
[{'https://serverfault.com': 143272}, {'https://stackoverflow.com': 191046}]
$python test.py
https://serverfault.com
https://stackoverflow.com
[{'https://serverfault.com': 143272}, {'https://stackoverflow.com': 191046}]
我尝试通过添加import asyncio、aiohttp
并将url
定义为url=['https://example.com']
。对我来说,脚本运行到完成时没有错误。你能用计算机重现这个问题吗?您可以在不同的计算机或不同的Python安装上复制它吗?请尝试:http://example.com/
我试过了,但行为是一样的-脚本完成时没有错误。嗯。。。。我会添加我的版本,可能有问题。而且实际的http usedIt也可能是Linux和Mac的不同之处——回溯看起来就像你在Mac上运行,而我在Linux上运行。
$ python test.py
https://serverfault.com <200>
https://stackoverflow.com <200>
[{'https://serverfault.com': 143272}, {'https://stackoverflow.com': 191046}]