Search code examples
pythonpython-asyncioaiohttp

aiohttp RuntimeError: await wasn't used with future


I'm aware of python asyncio - RuntimeError: await wasn't used with future, however the answer there doesn't address what went on here.

When using aiohttp I ran across the following error:

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/selector_events.py", line 485, in sock_connect
    return await fut
RuntimeError: await wasn't used with future
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7fb28040f760>

Here is the code associated:

urls = # some list of URLs including `https://www.alphavantage.co/query?function=OVERVIEW&symbol=IBM&apikey=demo`.....

async def run_tasks():
    session = aiohttp.ClientSession()
    tasks = [session.get(url) for url in urls]
    await asyncio.gather(*tasks)
    await session.close()

asyncio.run(run_tasks())

It looks like I am awaiting the event loop properly, but I keep hitting this issue, what gives?

Versions:

aiohttp==3.7.4.post0
asyncio==3.4.3
Python 3.8.0

Solution

  • There are 2 things that could be happening here:

    1. You are not awaiting something that you should be
    2. The URLs you're calling are bad requests, and the event loop is erroring before you get a chance to see what's really going on.

    According to the aiohttp documentation

    By default aiohttp uses strict checks for HTTPS protocol. Certification checks can be relaxed by setting ssl to False

    So it's likely that the urls that you're using are hitting this threshold, and you're getting kicked out. Change the line:

    tasks = [session.get(url) for url in urls]
    

    to

    tasks = [session.get(url, ssl=False) for url in urls]