My Python code returns unwanted response instead of it returning the programmed response and also says unable to close a running loop

  aiohttp, json, python-3.x, python-asyncio

My unedited 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()

It returns response like ({‘response’: "What’s up?"}, [‘response’])

How to modify code such that it returns response like What’s up?

Source: Python-3x Questions

LEAVE A COMMENT