My code says that it is unable to close a running loop

  aiohttp, python-3.x, python-asyncio

I made a code but it gives error unable to close a running loop

Code is

import aiohttp
import asyncio


async def get_response(query):
    async with aiohttp.ClientSession() as ses:
        async with ses.get(
            f'https://some-random-api.ml/chatbot?message={query}'
        ) as resp:
            return (await resp.json()),['response']
    
#using an event loop
loop = asyncio.get_event_loop()
Task = asyncio.gather(*[get_response('world') for _ in range(500)])

try:
    loop.run_until_complete(Task)
finally:
    loop.close()

Please modify the code for me because I’m a newly developer

I will be highly obliged if you help me out

Source: Python-3x Questions

LEAVE A COMMENT