Advertisement
Guest User

Async Bench: code with coroutines

a guest
Oct 3rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. import asyncio
  2. import aiohttp
  3.  
  4. LINK_FILE = open('links.txt', 'r')
  5. LINKS = LINK_FILE.readlines()
  6. LINKS_COUNT = len(LINKS)
  7.  
  8.  
  9. async def download_coroutine(img_url):
  10.     global LINKS_COUNT
  11.     async with aiohttp.ClientSession() as session:
  12.         async with session.get(img_url) as resp:
  13.             while True:
  14.                 chunk = await resp.content.read(1024 * 64)
  15.                 if not chunk:
  16.                     break
  17.                 # writing chunk to file is unnecessary for a benchmark
  18.     LINKS_COUNT -= 1
  19.     print(f'\rImages left: {LINKS_COUNT}   ', end='')
  20.  
  21.  
  22. async def main():
  23.     await asyncio.wait([download_coroutine(img_url.strip()) for img_url in LINKS])
  24.  
  25.  
  26. if __name__ == '__main__':
  27.     loop = asyncio.get_event_loop()
  28.     loop.run_until_complete(main())
  29.     loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement