title: Time asynchronous http requests using asyncio and timeit
body: <p>I found this code snippet from <a href="http://skipperkongen.dk/2016/09/09/easy-parallel-http-requests-with-python-and-asyncio/" rel="nofollow noreferrer">shipperkongen blog</a> regarding how to send concurrent requests using asyncio. I added in timeit to calculate the time used for the requests. However, it seems to wait for all requests to complete before proceeding to calculate the time needed. </p>
  
  <p>I would like to know how to get the time elapsed between the concurrent request sent and the first response. How should I modify the code to achieve this? Thanks</p>
  
  <p>Note that I added in the timeit lines and I am using Python3.6</p>
  
  <pre><code>import asyncio
  import concurrent.futures
  import requests
  import timeit
  
  async def main():
  
      with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
          loop = asyncio.get_event_loop()
          start = timeit.default_timer()
          futures = [
              loop.run_in_executor(
                  executor, 
                  requests.get, 
                  'http://example.org/'
              )
              for i in range(20)
          ]
          for response in await asyncio.gather(*futures):
              stop = timeit.default_timer()
              pass
          elapsed = stop - start
          print(elapsed)
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(main())
  </code></pre>
  
  <p>I got error that says "'return' with value in async generator" when I tried with <code>asyncio.as_completed(futures)</code></p>
  
  <pre><code>import asyncio
  import concurrent.futures
  import requests
  import timeit
  
  async def main():
  
      with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
          loop = asyncio.get_event_loop()
          start = timeit.default_timer()
          futures = [
              loop.run_in_executor(
                  executor, 
                  requests.get, 
                  'http://example.org/'
              )
              for i in range(20)
          ]
          for resp in asyncio.as_completed(futures):
              res = yield resp
              print(res.status_code)
              stop = timeit.default_timer()
              elapsed = stop - start
              print(elapsed)
              return elapsed
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(main())
  </code></pre>