Created
March 2, 2022 19:26
-
-
Save daviwesley/c321fbd4bb8931294be7c41011583c93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import httpx | |
import time | |
start_time = time.time() | |
async def get_pokemon(client, url): | |
resp = await client.get(url) | |
pokemon = resp.json() | |
return pokemon["name"] | |
async def main(): | |
async with httpx.AsyncClient() as client: | |
tasks = [] | |
for number in range(1, 151): | |
url = f"https://pokeapi.co/api/v2/pokemon/{number}" | |
tasks.append(asyncio.ensure_future(get_pokemon(client, url))) | |
original_pokemon = await asyncio.gather(*tasks) | |
for pokemon in original_pokemon: | |
print(pokemon) | |
asyncio.run(main()) | |
print("--- %s seconds ---" % (time.time() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment