import asyncio import time from aiohttp import ClientSession, ClientResponseError async def fetch_url_data(session, url): try: async with session.get(url, timeout=60) as response: resp = await response.read() except Exception as e: print(e) else: return resp return async def fetch_page(session, url): async with session.get(url) as response: return await response.json() async def main(): async with ClientSession() as session: tasks = [asyncio.ensure_future(fetch_page(session, url)) for url in urls] responses = await asyncio.gather(*tasks) for response in responses: print(response['fact']) if __name__ == '__main__': start_time = time.perf_counter() urls = [f'https://catfact.ninja/fact' for _ in range(100)] loop = asyncio.get_event_loop() loop.run_until_complete(main()) end_time = time.perf_counter() print(f'Total Time with async: {round(end_time - start_time, 5)} seconds.')