0

Is there anyway I can make all this requests at the same time? I'm trying to reduce the time...

def pokemons():
    
    for i in range(1, 800):
        url = f"https://pokeapi.co/api/v2/pokemon/{i}"
        requisicao = requests.get(url)

        try:
            lista = requisicao.json()
        except ValueError:
            print("ERRO TIPO")

1 Answers1

0

What you want is AIOHTTP Asynchronous HTTP Client/Server.

import asyncio
import aiohttp
import ssl


async def fetch(session, pokemon_num):
    url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_num}"
    async with session.get(url, ssl=ssl.SSLContext()) as response:
        return await response.json()


async def fetch_all(loop):
    async with aiohttp.ClientSession(loop=loop) as session:
        results = await asyncio.gather(*[fetch(session, pokemon_n) for pokemon_n in range(800)], return_exceptions=True)
        return results


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    result = loop.run_until_complete(fetch_all(loop))
    print(result)

I ran this code and got all the requests in a total of 19.5 seconds. I hope its good for your case.

The snippet above comes from another answer from YuriiKramarenko, if it suits you, you can give him a thumbs up. I adjusted it for your specific parameters.

Good luck catching them all!

Arson 0
  • 757
  • 2
  • 14
  • Thanks for that answer but when I put in my code didn't work. I get "There is no current event loop in thread 'Thread-1 (process_request_thread)'." everytime :/ – Pedro Abritta Jan 14 '22 at 14:19
  • Change the ''loop=asyncio.get_event_loop().'' to 'loop = asyncio.new_event_loop()' and 'asyncio.set_event_loop(loop)', update me of what happens. – Arson 0 Jan 14 '22 at 14:35
  • Also, try to run the snippet individually first to see if it works. If it works, try then putting it inside your project, otherwise it may be other parts of your code that are interfering with the task. – Arson 0 Jan 14 '22 at 14:38
  • It work! Thanks again for your help! – Pedro Abritta Jan 14 '22 at 15:06