-
-
Save Kavan72/e90de4d54647796b3aea830c07ea69a6 to your computer and use it in GitHub Desktop.
Revisions
-
khardix revised this gist
Oct 6, 2017 . No changes.There are no files selected for viewing
-
khardix created this gist
Oct 6, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ #!/usr/bin/env python3.6 import asyncio from contextlib import closing import aiohttp import tqdm async def download(session, url, progress_queue): async with session.get(url) as response: target = url.rpartition('/')[-1] size = int(response.headers.get('content-length', 0)) or None position = await progress_queue.get() progressbar = tqdm.tqdm( desc=target, total=size, position=position, leave=False, ) with open(target, mode='wb') as f, progressbar: async for chunk in response.content.iter_chunked(512): f.write(chunk) progressbar.update(len(chunk)) await progress_queue.put(position) return target async def main(loop): with open('urls.txt') as f: urls = [url.strip() for url in f] progress_queue = asyncio.Queue(loop=loop) for pos in range(5): progress_queue.put_nowait(pos) async with aiohttp.ClientSession(loop=loop) as session: tasks = [download(session, url, progress_queue) for url in urls] return await asyncio.gather(*tasks) with closing(asyncio.get_event_loop()) as loop: for tgt in loop.run_until_complete(main(loop)): print(tgt)