Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Integralist/229eaa0a688773e1cb5f5ea03facee6f to your computer and use it in GitHub Desktop.
Save Integralist/229eaa0a688773e1cb5f5ea03facee6f to your computer and use it in GitHub Desktop.
Multiple asynchronous HTTP GET requests with Python's aiohttp and asyncio
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))
@bradwood
Copy link

you are creating a session for each and every get(). Which is not ideal as per the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment