From 56094ef0f00513ebb0b72e550cc8cc0bd9181b7f Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Tue, 14 Jul 2015 00:22:05 -0300 Subject: [PATCH] flags2_asyncio with closing rehttp response --- 17-futures/countries/flags2_asyncio.py | 4 ++-- 17-futures/countries/flags2_asyncio_executor.py | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/17-futures/countries/flags2_asyncio.py b/17-futures/countries/flags2_asyncio.py index e7477b5..d3c78f8 100644 --- a/17-futures/countries/flags2_asyncio.py +++ b/17-futures/countries/flags2_asyncio.py @@ -18,7 +18,7 @@ Sample run:: # BEGIN FLAGS2_ASYNCIO_TOP import asyncio import collections -from contextlib import closing +import contextlib import aiohttp from aiohttp import web @@ -41,7 +41,7 @@ class FetchError(Exception): # <1> def get_flag(base_url, cc): # <2> url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower()) resp = yield from aiohttp.request('GET', url) - with closing(resp): + with contextlib.closing(resp): if resp.status == 200: image = yield from resp.read() return image diff --git a/17-futures/countries/flags2_asyncio_executor.py b/17-futures/countries/flags2_asyncio_executor.py index bcb7f28..6822c24 100644 --- a/17-futures/countries/flags2_asyncio_executor.py +++ b/17-futures/countries/flags2_asyncio_executor.py @@ -81,16 +81,14 @@ def downloader_coro(cc_list, base_url, verbose, concur_req): error_msg = exc.__cause__.args[0] except IndexError: error_msg = exc.__cause__.__class__.__name__ + if verbose and error_msg: + msg = '*** Error for {}: {}' + print(msg.format(country_code, error_msg)) + status = HTTPStatus.error 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