【发布时间】:2021-04-11 20:50:40
【问题描述】:
我知道python asyncio - RuntimeError: await wasn't used with future,但是那里的答案并没有解决这里发生的事情。
使用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>
这是相关的代码:
urls = # some list of URLs including `https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=demo`.....
async def run_tasks():
session = aiohttp.ClientSession()
tasks = [session.get(url) for url in urls]
await asyncio.gather(*tasks)
await session.close()
asyncio.run(run_tasks())
它看起来就像我正在正确地等待事件循环,但我一直遇到这个问题,这是什么原因?
版本:
aiohttp==3.7.4.post0
asyncio==3.4.3
Python 3.8.0
【问题讨论】:
-
我尝试通过添加
import asyncio, aiohttp并将urls定义为urls = ['https://example.com']来重现您的问题。对我来说,脚本运行完成没有错误。你能用that code重现这个问题吗?你能在不同的计算机或不同的 Python 安装上重现它吗? -
试试:
http://example.com/ -
我试过了,行为是一样的——脚本完成没有错误。
-
嗯....我会添加我的版本,也许那里有问题。还有实际使用的http
-
这也可能是 Linux 与 Mac 的区别 - 回溯看起来像您在 Mac 上运行,而我在 Linux 上。
标签: python python-asyncio aiohttp