python - 等待多个 aiohttp 请求导致“会话已关闭”错误
问题描述
我正在编写一个辅助类,用于以异步方式处理多个 url 请求。代码如下。
class urlAsyncClient(object):
def __init__(self, url_arr):
self.url_arr = url_arr
async def async_worker(self):
result = await self.__run()
return result
async def __run(self):
pending_req = []
async with aiohttp.ClientSession() as session:
for url in self.url_arr:
r = self.__fetch(session, url)
pending_req.append(r)
#Awaiting the results altogether instead of one by one
result = await asyncio.wait(pending_req)
return result
@staticmethod
async def __fetch(session, url):
async with session.get(url) as response: #ERROR here
status_code = response.status
if status_code == 200:
return await response.json()
else:
result = await response.text()
print('Error ' + str(response.status_code) + ': ' + result)
return {"error": result}
在异步中,一一等待结果似乎毫无意义。我将它们放入一个数组中,然后一起等待await asyncio.wait(pending_req)
。
但似乎这不是正确的方法,因为我收到以下错误
在 __fetch async with session.get(url) 作为响应: RuntimeError: Session is closed
我可以知道正确的方法吗?谢谢。
解决方案
因为会话在您等待之前已经关闭
async with aiohttp.ClientSession() as session:
for url in self.url_arr:
r = self.__fetch(session, url)
pending_req.append(r)
#session closed hear
你可以让 session 成为一个参数__run
,就像这样
async def async_worker(self):
async with aiohttp.ClientSession() as session:
result = await self.__run(session)
return result
# session will close hear
async def __run(self, session):
pending_req = []
for url in self.url_arr:
r = self.__fetch(session, url)
pending_req.append(r)
#Awaiting the results altogether instead of one by one
result = await asyncio.wait(pending_req)
return result
推荐阅读
- asp.net-core - Asp .net 核心 - 无法加载文件或程序集“ServiceStack”或其依赖项之一
- php - PHP - 使用来自多数组键搜索的值创建日期范围数组
- java - 无法访问子类的方法
- reactjs - 安装 React 项目时遇到问题
- swift - 快速在字典中获取数组值
- android - Megacool SDK、推荐系统、receiveShareOpened 和 sentShareOpened 永远不会被调用
- python - UnboundLocalError:在分配之前引用了局部变量“temp_var”
- swift - Eureka in swift List Selection Selected Value 导航离开时自动取消选择
- laravel - Laravel 5.8 与任何其他命令一起运行调度程序
- flutter - 如何遍历集合中的所有文档以在 ListView 中显示内容?