Skip to content

Instantly share code, notes, and snippets.

@leotada
Created April 1, 2019 19:41
Show Gist options
  • Save leotada/18e4d7ddb55b974e88b6ed4a32f98f6c to your computer and use it in GitHub Desktop.
Save leotada/18e4d7ddb55b974e88b6ed4a32f98f6c to your computer and use it in GitHub Desktop.
Async request with aiohttp
import aiohttp
import asyncio
async def fetch(session, url):
async with session.get(url) as response:
print(response.status)
async def main():
url = 'http://localhost:8888'
async with aiohttp.ClientSession() as session:
requests_corroutines = []
for _ in range(5):
requests_corroutines.append(fetch(session, url))
futures = asyncio.gather(*requests_corroutines)
await futures
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment