Skip to content

Instantly share code, notes, and snippets.

@Nagasaki45
Last active March 14, 2016 16:13
Show Gist options
  • Save Nagasaki45/176f2225b59aa2b0f814 to your computer and use it in GitHub Desktop.
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
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