[Python] Python package - aiohttp has a warning message "Unclosed client session"

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página