Awaiting multiple aiohttp requests cause 'Session is closed' error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Awaiting multiple aiohttp requests cause 'Session is closed' error



I am writing a helper class for handling multiple urls request in asynchronous way. The code is following.


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



As awaiting the result one by one seems meaningless in asynchronous. I put them into an array and wait together by await asyncio.wait(pending_req).


await asyncio.wait(pending_req)



But seems like it is not the correct way to do it as I get the following error



in __fetch async with session.get(url) as response: RuntimeError: Session is closed



May I know the correct way to do it? Thanks.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard