Created
May 16, 2019 11:13
-
-
Save lokesh1729/7b50d94475b5b29855e1137a6c9443a5 to your computer and use it in GitHub Desktop.
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 characters
import asyncio | |
import json | |
import time | |
import aiohttp | |
result = [] | |
async def download_site(session, url): | |
async with session.get(url) as response: | |
resp_json = await response.json() | |
print("url", url, resp_json) | |
result.append(resp_json) | |
async def download_all_sites(_sites): | |
async with aiohttp.ClientSession() as session: | |
tasks = [] | |
for url in _sites: | |
task = asyncio.ensure_future(download_site(session, url)) | |
tasks.append(task) | |
await asyncio.gather(*tasks, return_exceptions=True) | |
if __name__ == "__main__": | |
sites = [ | |
"https://reqres.in/api/users", | |
"http://dummy.restapiexample.com/api/v1/employees", | |
] | |
start_time = time.time() | |
asyncio.get_event_loop().run_until_complete(download_all_sites(sites)) | |
# print(result) | |
print("total time took is %s" % (time.time() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment