-
-
Save aalllq/db498d6a69ecdf0fe3097ba8e7240cc1 to your computer and use it in GitHub Desktop.
Revisions
-
Den1al renamed this gist
Feb 13, 2018 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Den1al revised this gist
Dec 20, 2017 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,11 @@ async def fetch_all(urls: list): """ Fetch all URLs """ tasks = [] custom_headers = { 'User-Agent': 'aiohttp client 0.17' } async with ClientSession(headers=custom_headers) as session: for url in urls: task = asyncio.ensure_future(fetch(url, session)) tasks.append(task) @@ -42,16 +45,17 @@ def main(): urls = [ 'https://twitter.com', 'https://9to5mac.com', 'https://amazon.com' ] loop = asyncio.get_event_loop() total_future = asyncio.ensure_future(fetch_all(urls)) loop.run_until_complete(total_future) total_elapsed = default_timer() - start_time print(f'{" total time of".rjust(28, "-")}: {total_elapsed:5.2f}') if __name__ == "__main__": main() -
Den1al revised this gist
Dec 20, 2017 . No changes.There are no files selected for viewing
-
Den1al created this gist
Dec 19, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,57 @@ # author: @Daniel_Abeles # date: 18/12/2017 import asyncio from aiohttp import ClientSession from timeit import default_timer import async_timeout async def fetch_all(urls: list): """ Fetch all URLs """ tasks = [] async with ClientSession() as session: for url in urls: task = asyncio.ensure_future(fetch(url, session)) tasks.append(task) _ = await asyncio.gather(*tasks) async def fetch(url: str, session: object): """ Fetch a single URL """ with async_timeout.timeout(10): async with session.get(url) as response: before_request = default_timer() resp = await response.read() elapsed = default_timer() - before_request print(f'{url:30}{elapsed:5.2f}') return { 'resp': resp, 'url': url, 'elapsed': elapsed } def main(): """ Main Function """ start_time = default_timer() urls = [ 'https://twitter.com', 'https://9to5mac.com', 'https://amazon.com', ] * 10 loop = asyncio.get_event_loop() total_future = asyncio.ensure_future(fetch_all(urls)) loop.run_until_complete(total_future) total_elapsed = default_timer() - start_time print(f'{" total time of".rjust(28, "-")}: {total_elapsed:5.2f}') if __name__ == "__main__": main()