等待多个 aiohttp 请求导致 'Session is closed' 错误
Awaiting multiple aiohttp requests cause 'Session is closed' error
我正在编写一个帮助程序 class 以异步方式处理多个 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)
.
但似乎这不是正确的方法,因为我收到以下错误
in __fetch async with session.get(url) as response: 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
你可以使会话成为 __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
我正在编写一个帮助程序 class 以异步方式处理多个 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)
.
但似乎这不是正确的方法,因为我收到以下错误
in __fetch async with session.get(url) as response: 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
你可以使会话成为 __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