SHARE
TWEET

Untitled

a guest Oct 17th, 2019 81 in 333 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import asyncio
  3. import aiohttp
  4.  
  5. URL = 'https://api.github.com/events'
  6. NUM_CONNECTIONS = 10
  7.  
  8. async def aio_get(url):
  9.     async with aiohttp.ClientSession() as session:
  10.         async with session.get(url) as resp:
  11.             return resp
  12.  
  13. async def run(url, i):
  14.     print(f'Staring task {i}')
  15.     start = time.time()
  16.     http = await aio_get(url)
  17.     elapsed = time.time() - start
  18.     print(f'Task finished {i}, {elapsed:.1f}s')
  19.     http.close()
  20.  
  21. async def async_main(url, _max):
  22.     start = time.time()
  23.     tasks = [asyncio.create_task(run(url, i)) for i in range(0, _max)]
  24.     await asyncio.gather(*tasks)
  25.     elapsed = time.time() - start
  26.     print(f'All tasks finished, {elapsed:.1f}s')
  27.  
  28. asyncio.run(async_main(URL, NUM_CONNECTIONS))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top