{"id":974,"hash":"041ac9882d5f7acd319bf46a5d5f8ace41ecfa8d5c1c0533efac96e43cd3e89d","pattern":"Python package - aiohttp has a warning message &quot;Unclosed client session&quot;","full_message":"My code is as follows:\n\nimport asyncio\nimport aiohttp\n\nurls = [\n    'http://www.163.com/',\n    'http://www.sina.com.cn/',\n]\n\nasync def get_url_data(u):\n    resp = await aiohttp.ClientSession().get(url=u)\n    headers = resp.headers\n    return headers\n\nasync def request_url(u):\n    res = await get_url_data(u)\n    return res\n\nloop = asyncio.get_event_loop()\ntask_lists = asyncio.wait([request_url(u) for u in urls])\nloop.run_until_complete(task_lists)\nloop.close()\n\nWhen I running my code, it's display a warning message:\nUnclosed client session\n\nAnybody can give me some solutions about that?","ecosystem":"pypi","package_name":"python-asyncio","package_version":null,"solution":"You should close the connection in the end.\nYou have 2 options:\n\nYou can close the connection manually:\n\nimport aiohttp\nsession = aiohttp.ClientSession()\n# use the session here\nawait session.close()\n\nOr you can use it with a context manager:\n\nimport aiohttp\nimport asyncio\n\nasync def fetch(client):\n    async with client.get('http://python.org') as resp:\n        assert resp.status == 200\n        return await resp.text()\n\nasync def main(loop):\n    async with aiohttp.ClientSession(loop=loop) as client:\n        html = await fetch(client)\n        print(html)\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(main(loop))\n\nThe client session supports the context manager protocol for self closing.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/46112848/python-package-aiohttp-has-a-warning-message-unclosed-client-session","votes":28,"created_at":"2026-04-19T04:52:07.350138+00:00","updated_at":"2026-04-19T04:52:07.350138+00:00"}