#!/usr/bin/env python#-*-coding:utf-8-*-__author__ = "Daniel Altiparmak ([email protected])" __copyright__ = "Cop Yright (C) Daniel altiparmak "__license__ =" GPL 3.0 "Import asyncioimport aiohttpimport tqdmimport stringimport Random # get content and write it to FileDef write_to_file (filename, content): F = open (filename, ' WB ') f.write (content) F.close () # A helper coroutine to perform get requests: @asyncio. Coroutinedef get (*args, **kwargs): Response = yield fro M aiohttp.request (' GET ', *args, **kwargs) return (yield from response.read_and_close ()) @asyncio. Coroutinedef Download_ File (URL): # This routine was protected by a semaphore with (yield from r_semaphore): content = yield from asy Ncio.async (Get (URL)) # create random filename length = ten file_string = '. Join (Random.choice ( String.ascii_lowercase + string.digits) for _ in range (length) filename = ' {}.P ng '. Format (file_string) Write_to_file (filename, content) ' Make nice progressbarinstall it by using ' pip install TQDM ' @asyncio. Coroutinede F Wait_with_progressbar (Coros): For F in TQDM.TQDM (asyncio.as_completed (Coros), Total=len (Coros)): Yield from fi mages = [' http://lorempixel.com/1920/1920/' for I in range (+)]# avoid to many requests (Coroutines) the same time.# limit them by setting semaphores (simultaneous requests) R_semaphore = Asyncio. Semaphore (Ten) coroutines = [Download_file (URL) for URL in images]eloop = Asyncio.get_event_loop () #eloop. Run_until_ Complete (asyncio.wait (coroutines)) Eloop.run_until_complete (Wait_with_progressbar (coroutines)) Eloop.close ()
Aiohttp/asyncio Multiple Requests