Skip to content

Instantly share code, notes, and snippets.

@agners
Last active May 9, 2022 11:48
Show Gist options
  • Save agners/6bd63fe0800335f41a4b8cb03574cf2b to your computer and use it in GitHub Desktop.
Save agners/6bd63fe0800335f41a4b8cb03574cf2b to your computer and use it in GitHub Desktop.
asyncio aiohttp timeout
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