Last active
March 14, 2016 16:13
-
-
Save Nagasaki45/176f2225b59aa2b0f814 to your computer and use it in GitHub Desktop.
Async http client with asyncio and aiohttp. Example for fetching muliple pages cuncurrently
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 | |
import time | |
urls = ['http://python.org', 'http://www.google.com'] | |
d = {} | |
async def fetch(client, url): | |
print('start fetching {}: {}'.format(url, time.time() - start)) | |
async with client.get(url) as resp: | |
d[url] = await resp.text() | |
print('done fetching {}: {}'.format(url, time.time() - start)) | |
start = time.time() | |
with aiohttp.ClientSession() as client: | |
tasks = asyncio.wait([fetch(client, url) for url in urls]) | |
asyncio.get_event_loop().run_until_complete(tasks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment