Python package - aiohttp has a warning message "Unclosed client session"
Asked Answered
I

4

25

My code is as follows:

import asyncio
import aiohttp

urls = [
    'http://www.163.com/',
    'http://www.sina.com.cn/',
]

async def get_url_data(u):
    resp = await aiohttp.ClientSession().get(url=u)
    headers = resp.headers
    return headers


async def request_url(u):
    res = await get_url_data(u)
    return res

loop = asyncio.get_event_loop()
task_lists = asyncio.wait([request_url(u) for u in urls])
loop.run_until_complete(task_lists)
loop.close()

When I running my code, it's display a warning message: Unclosed client session

Anybody can give me some solutions about that?

Imbibition answered 8/9, 2017 at 9:14 Comment(0)
V
30

You should close the connection in the end. You have 2 options:

You can close the connection manually:

import aiohttp
session = aiohttp.ClientSession()
# use the session here
session.close()

Or you can use it with a contex manager:

import aiohttp
import asyncio

async def fetch(client):
    async with client.get('http://python.org') as resp:
        assert resp.status == 200
        return await resp.text()

async def main(loop):
    async with aiohttp.ClientSession(loop=loop) as client:
        html = await fetch(client)
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))

The client session supports the context manager protocol for self closing.

Vinnievinnitsa answered 8/9, 2017 at 16:18 Comment(2)
re the manual option, should that be await session.close() ?Cleodel
Yes, the session.close() is a coroutine and thus simply using session.close() will do nothingFaretheewell
H
14

If you are not using context manager, the proper way to close it would also need an await. Many answers on the internet miss that part, and few people actually notice it, presumably because most people use the more convenient context manager. But the manual await session.close() is essential when/if you are closing a class-wide session inside the tearDownClass() when doing unittesting.

import aiohttp
session = aiohttp.ClientSession()
# use the session here
await session.close()
Heddi answered 21/2, 2020 at 5:50 Comment(1)
Ah yes, I forgot to add await to the session.close() because I am in an async function.Linger
C
5

You should use ClientSession using async context manager for proper blocking/freeing resources:

async def get_url_data(u):
    """
    read url data
    :param u:
    :return:
    """
    print('running ', u)
    async with aiohttp.ClientSession() as session:
        resp = await session.get(url=u)
        headers = resp.headers
        print(u, headers)
        return headers
Conventual answered 8/9, 2017 at 10:20 Comment(0)
A
0

Read this pls: https://docs.aiogram.dev/en/latest/api/session/aiohttp.html

from aiogram import Bot
from aiogram.client.session.aiohttp import AiohttpSession

session = AiohttpSession()
bot = Bot('token', session=session)

That will solve your problem. And yes, the session does not need to be closed

Akihito answered 30/5 at 13:57 Comment(2)
What page exactly? The link is cut off.Bumbling
The code example seems to be from docs.aiogram.dev/en/latest/api/session/aiohttp.html so I fixed their link to be thatSuborn

© 2022 - 2024 — McMap. All rights reserved.