Skip to content

Instantly share code, notes, and snippets.

@lokesh1729
Created May 16, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokesh1729/7b50d94475b5b29855e1137a6c9443a5 to your computer and use it in GitHub Desktop.
Save lokesh1729/7b50d94475b5b29855e1137a6c9443a5 to your computer and use it in GitHub Desktop.
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