Created
April 1, 2019 19:41
-
-
Save leotada/18e4d7ddb55b974e88b6ed4a32f98f6c to your computer and use it in GitHub Desktop.
Async request with aiohttp
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 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