Search code examples
pythonpython-asyncioaiohttp

Python asyncio response time


I am trying to learn asyncio and aiohttp. I have the following code to query for a bunch of URLs using asyncio.

import aiohttp
import asyncio

sync def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    urls = [
            'http://yahoo.com',
            'https://google.com',
            'http://bing.com'
        ]
    tasks = []
    async with aiohttp.ClientSession() as session:
        for url in urls:
            tasks.append(fetch(session, url))
        htmls = await asyncio.gather(*tasks)
        for html in htmls:
            print(html[:100] + "\n")
    
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

How do I get the response time for each asynchronous query? My first idea was to just use a timer, but I don't know how to use that in this situation. I've tried searching for if anybody has done something similar, but can't find anything.


Solution

  • async def fetch(session, url):
        """[Coroutine to send HTTP request to URLs provided]
    
        Args:
            session ([aiohttp Client Session object]): [session object]
            url ([string]): [URL to query]
    
        Returns:
            [list]: [response url, status and time taken]
        """
        tic = time.perf_counter() # Start timer
        try:
            response = await session.request(method='GET', url=url, timeout=10)
            toc = time.perf_counter() # Stop timer
            time_taken  = toc - tic # Calculate time taken to get response
            response.raise_for_status()
            print("URL : ", url)
            print("HTTP Status : ", response.status)
            print(f"Time taken : {time_taken:4f} seconds")
            print()
            return str(response.url), response.status, time_taken
        
        except HTTPError as http_err:
            logging.error(http_err)
        except Exception as err:
            logging.error(err)