Search code examples
pythonaiohttp

Python aiohttp - how to close a session?


I am totally new to async. I'm having trouble closing connections when trying to make async requests. Here is my code:

def async_aiohttp_get_all(self, urls):
    """
    performs asynchronous get requests
    """
    async def get_all(urls):
        async with aiohttp.ClientSession(connector_owner=False) as session:
            async def fetch(url):
                async with session.get(url) as response:
                    #return await response.json()
                    rtn = {"url": response.url,
                           "content": await response.text()}
                    return rtn
            rslts = await asyncio.gather(*[
                fetch(url) for url in urls
            ])
            return rslts
    # call get_all as a sync function to be used in a sync context
    return sync.async_to_sync(get_all)(urls)

The error I'm getting is:

Unclosed connector
connections: ['[(<aiohttp.client_proto.ResponseHandler object at 0x0000024EE21F4890>, 10368.656), (<aiohttp.client_proto.ResponseHandler object at 0x0000024EE21F4770>, 10368.671), (<aiohttp.client_proto.ResponseHandler object at 0x0000024EE21F49B0>, 10368.687), (<aiohttp.client_proto.ResponseHandler object at 0x0000024EE21F4AD0>, 10368.75), (<aiohttp.client_proto.ResponseHandler object at 0x0000024EE2261D90>, 10369.078)]']
connector: <aiohttp.connector.TCPConnector object at 0x0000024EE280C260>

I thought the solution must be to add await session.close(), which I tried on the line before return rslts but this didn't make any difference.

Anyone can help me?


Solution

  • The with keyword will gracefully close your client connection, so the problem could possible be with connector_owner=False remove it.

     async with aiohttp.ClientSession() as session:
        async def fetch(url):
           async with session.get(url) as response:
                 #return await response.json()
                 rtn = {"url": response.url,"content": await response.text()}
                 return rtn