Created
November 1, 2016 15:37
-
-
Save Integralist/229eaa0a688773e1cb5f5ea03facee6f to your computer and use it in GitHub Desktop.
Multiple asynchronous HTTP GET requests with Python's aiohttp and asyncio
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 time | |
import datetime | |
import asyncio | |
import aiohttp | |
domain = 'http://integralist.co.uk' | |
a = '{}/foo?run={}'.format(domain, time.time()) | |
b = '{}/bar?run={}'.format(domain, time.time()) | |
async def get(url): | |
print('GET: ', url) | |
async with aiohttp.ClientSession() as session: | |
async with session.get(url) as response: | |
t = '{0:%H:%M:%S}'.format(datetime.datetime.now()) | |
print('Done: {}, {} ({})'.format(t, response.url, response.status)) | |
loop = asyncio.get_event_loop() | |
tasks = [ | |
asyncio.ensure_future(get(a)), | |
asyncio.ensure_future(get(b)) | |
] | |
loop.run_until_complete(asyncio.wait(tasks)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you are creating a session for each and every
get()
. Which is not ideal as per the docs.