0

I always see some unexpected behavior when I use async code in python. For example I get a lot of RuntimeError: Event loop is closed after running this simple code. Can someone explain what I'm doing wrong?

from asyncio import run, wait
from datetime import datetime

import aiohttp

url = "https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=cat"

start = datetime.now()


async def load(i):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            pass
    print("Downloaded", i + 1, datetime.now() - start)


async def main():
    reqs = []
    for i in range(100):
        reqs.append(load(i))
    await wait(reqs)


run(main())
  • The code itself works for me. Do you get this code when executing this script or when doing something else with it? – Simon Hawe Jan 27 '22 at 11:01
  • That the whole code without any changes. It's works for me too, but I get errors after code is done running. I'm running it on Windows – Andrei Krasnikau Jan 27 '22 at 15:02

0 Answers0