Last active
May 9, 2022 11:48
-
-
Save agners/6bd63fe0800335f41a4b8cb03574cf2b to your computer and use it in GitHub Desktop.
asyncio aiohttp timeout
This file contains hidden or 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 aiohttp | |
import asyncio | |
async def work(i: int): | |
#print(f"start working on {i}") | |
for i in range(50): | |
pass | |
async def main(): | |
tasks = [] | |
for i in range(10000000): | |
tasks.append(asyncio.create_task(work(i))) | |
async with aiohttp.ClientSession() as session: | |
pokemon_url = 'https://pokeapi.co/api/v2/pokemon/151' | |
print("doing get request") | |
async with session.get(pokemon_url, timeout=5) as resp: | |
pokemon = await resp.json() | |
print(pokemon['name']) | |
asyncio.gather(*tasks) | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment