verbose) # <9> to_do_map[future] = cc # <10> done_iter = futures.as_completed(to_do_map) # <11> if not verbose: done_iter = tqdm.tqdm(done_iter, total=len(cc_list)) # <12> for future in done_iter: # <13> try: res = future.result() # <14> except requests.exceptions.HTTPError as exc: # <15> error_msg = 'HTTP {res.status_code} - {res.reason}' error_msg = error_msg.format(res=exc.response) except requests.exceptions.ConnectionError as exc: error_msg = 'Connection error' else: error_msg = '' status = res.status if error_msg: status = HTTPStatus.error counter[status] += 1 if verbose and error_msg: cc = to_do_map[future] # <16> print('*** Error for {}: {}'.format(cc, error_msg)) return counter if __name__ == '__main__': main(download_many, DEFAULT_CONCUR_REQ, MAX_CONCUR_REQ) # END FLAGS2_THREADPOOL
country_code = exc.country_code try: error_msg = exc.__cause__.args[0] except IndexError: error_msg = exc.__cause__.__class__.__name__ else: error_msg = '' status = res.status if error_msg: status = HTTPStatus.error counter[status] += 1 if verbose and error_msg: msg = '*** Error for {}: {}' print(msg.format(country_code, error_msg)) return counter def download_many(cc_list, base_url, verbose, concur_req): loop = asyncio.get_event_loop() coro = downloader_coro(cc_list, base_url, verbose, concur_req) counts = loop.run_until_complete(coro) loop.close() return counts if __name__ == '__main__': main(download_many, DEFAULT_CONCUR_REQ, MAX_CONCUR_REQ)
#print(url) #resp = yield from aiohttp.request('GET', url) #image = yield from resp.read() async with session.get(url) as resp: image = await resp.read() return image #@asyncio.coroutine async def download_one(cc): #image = yield from get_flag(cc) async with aiohttp.ClientSession() as session: image = await (get_flag(session, cc)) show(cc) save_flag(image, cc.lower() + '.png') return cc def download_many(cc_list): loop = asyncio.get_event_loop() to_do = [download_one(cc) for cc in sorted(cc_list)] wait_coro = asyncio.wait(to_do) res, _ = loop.run_until_complete(wait_coro) loop.close() return len(res) if __name__ == '__main__': main(download_many)