Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!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)
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
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.
await session.close()
return rslts
Anyone can help me?
Tags: python,aiohttpSource of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
On this page of the site you can watch the video online Python aiohttp - how to close a session? with a duration of hours minute second in good quality, which was uploaded by the user Emrah KAYA 18 October 2024, share the link with friends and acquaintances, this video has already been watched 27 times on youtube and it was liked by 0 viewers. Enjoy your viewing!