1

I have the following code which I'm trying to run to get data from an api asynchronously, using asyncio and aiohttp:

import asyncio
import aiohttp

api = "...some api..."
apps = [
    ...list of api parameters...
]

def getTasks(sess):
    tasks = list()
    for app in apps:
        tasks.append(asyncio.create_task(sess.get(api+app, ssl = False)))
    return tasks

async def main():
    results = list()
    async with aiohttp.ClientSession() as atpSession:
        tasks = getTasks(atpSession)
        responses = await asyncio.gather(*tasks)
        for response in responses:
            results.append(await response.json())
    print(results[-1])
    print("Done!")

if __name__ == "__main__":
    asyncio.run(main())

Though I'm getting the response data, but the following error keeps popping up:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001C5D98F7490>
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Program Files\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

And there are multiple such similar tracebacks shown one by one.

Now another way I tried this was by removing asyncio.run(main()) and just using some different lines of code:

import asyncio
import aiohttp

api = "...some api..."
apps = [
    ...list of api parameters...
]

def getTasks(sess):
    tasks = list()
    for app in apps:
        tasks.append(asyncio.create_task(sess.get(api+app, ssl = False)))
    return tasks

async def main():
    results = list()
    async with aiohttp.ClientSession() as atpSession:
        tasks = getTasks(atpSession)
        responses = await asyncio.gather(*tasks)
        for response in responses:
            results.append(await response.json())
    print(results[-1])
    print("Done!")

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Using the following did not give me the prior error but gave me:

DeprecationWarning: There is no current event loop
  loop = aio.get_event_loop()

Although, it did give me the responses, my question is, why are these differences arising? Being an absolute beginner with asyncio, I had read that as application developers, we should be using the high level apis like asyncio.run() instead of the low level apis, so why is asyncio.run() creating such problems?

  • Can you please post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and an exact error traceback? – matszwecja Feb 21 '23 at 15:56
  • To add to the above, where and how are you running your code? There are known Jupyter Notebook versions where the asyncio loop is opened & closed before user code execution. – felipe Feb 22 '23 at 08:42
  • I've edited and put the entire code and traceback there. Nothing more to it, that's just all. – Biswadeep Sarkar Feb 22 '23 at 13:06
  • And also, I'm not using jupyter notebook, I'm plainly using the vscode editor and the git bash terminal – Biswadeep Sarkar Feb 22 '23 at 13:10

1 Answers1

0

You don't need the asyncio.create_task() and this is likely the cause of the error. Take a look at this answer for a working example of AsyncIO + AIOHTTP.

Louis Lac
  • 5,298
  • 1
  • 21
  • 36
  • Tried to run the code in the answer that you mentioned. Still getting the same traceback as I mentioned in my question: RuntimeError: Event loop is closed. – Biswadeep Sarkar Feb 23 '23 at 09:15
  • Works fine on my side, what Python version are you using? How do you launch the program? What is your OS? – Louis Lac Feb 23 '23 at 11:04
  • Python 3.10.5 running the program through the gitbash terminal OS is windows – Biswadeep Sarkar Feb 23 '23 at 11:21
  • Weird. Try running the code in an isolated Python environment (with pyenv, venv or conda for instance). Also try with another version of Python. – Louis Lac Feb 23 '23 at 17:18