From bbae20e24c2470d385232873017a1076be23e149 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sat, 14 Oct 2017 15:55:51 -0700 Subject: [PATCH] Do HTTP requests concurrently --- facet/cli.py | 9 ++++++--- facet/core.py | 13 ++++++++----- setup.py | 1 + 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/facet/cli.py b/facet/cli.py index 2832b47..e6b5ddd 100644 --- a/facet/cli.py +++ b/facet/cli.py @@ -1,3 +1,4 @@ +import asyncio import os import shutil import subprocess @@ -177,9 +178,11 @@ def fetch(self, options): else: include_inactive = options.get('--all') facets = Facet.get_all(include_inactive) - for facet in facets: - facet.fetch() - print(facet.format()) + + ioloop = asyncio.get_event_loop() + task = asyncio.wait([facet.fetch() for facet in facets]) + ioloop.run_until_complete(task) + ioloop.close() def follow(self, options): """ diff --git a/facet/core.py b/facet/core.py index 2d2acc2..c8d75af 100644 --- a/facet/core.py +++ b/facet/core.py @@ -4,7 +4,7 @@ from os import listdir from os import path -import requests +import aiohttp import yaml from facet import settings @@ -76,13 +76,16 @@ def apply_patch(self, patch): config.update(self.read_config()) self.write_config(config) - def fetch(self): + async def fetch(self): if not self.jira: return - resp = requests.get(self.jira_json_url) - resp.raise_for_status() + async with aiohttp.ClientSession() as session: + resp = await session.get(self.jira_json_url) + resp.raise_for_status() + data = await resp.json() with open(self.jira_data_file, 'w') as fp: - dump_json(resp.json(), fp) + dump_json(data, fp) + print(self.format()) def format(self): if self.jira: diff --git a/setup.py b/setup.py index 7e12e36..8e2a5b0 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ ], }, install_requires=[ + 'aiohttp', 'clint', 'docopt', 'pyyaml',