我正在编写一个帮助程序类,以异步方式处理多个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异步中,使用session.get(url)作为响应:RuntimeError:会话已关闭
我可以知道正确的方法吗?谢谢。
答案 0 :(得分:1)
因为会话在您等待之前已经关闭
async with aiohttp.ClientSession() as session:
for url in self.url_arr:
r = self.__fetch(session, url)
pending_req.append(r)
#session closed hear
您可以像这样将会话设为__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