Skip to content

Instantly share code, notes, and snippets.

@sultaniman
Created May 16, 2018 15:53
Show Gist options
  • Select an option

  • Save sultaniman/cfa60bc9e65d86ee2d2c2430785e5949 to your computer and use it in GitHub Desktop.

Select an option

Save sultaniman/cfa60bc9e65d86ee2d2c2430785e5949 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""AsyncIO"""
import aiohttp
import asyncio
import async_timeout
import json
links = [
"http://localhost:4000/106410.json",
"http://localhost:4000/106430.json",
"http://localhost:4000/106451.json",
"http://localhost:4000/106486.json",
"http://localhost:4000/106536.json",
"http://localhost:4000/106541.json",
"http://localhost:4000/106552.json",
"http://localhost:4000/106554.json",
"http://localhost:4000/106571.json",
"http://localhost:4000/106753.json",
"http://localhost:4000/106754.json",
"http://localhost:4000/106757.json",
"http://localhost:4000/106787.json",
"http://localhost:4000/106796.json",
"http://localhost:4000/106801.json",
"http://localhost:4000/106821.json",
"http://localhost:4000/106925.json",
"http://localhost:4000/106945.json",
"http://localhost:4000/106964.json",
"http://localhost:4000/106970.json",
"http://localhost:4000/106974.json",
"http://localhost:4000/106975.json"
]
async def fetch(link: str, session: aiohttp.ClientSession):
with async_timeout.timeout(10):
async with session.get(link) as res:
print(f'Fetching {link}')
return await res.json()
async def fetch_all(urls):
async with aiohttp.ClientSession() as client:
results = await asyncio.gather(*[
fetch(link, client)
for link in urls
], return_exceptions=True)
return results
loop = asyncio.get_event_loop()
results = loop.run_until_complete(fetch_all(links))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment