flags2_asyncio with closing rehttp response

This commit is contained in:
Luciano Ramalho 2015-07-14 00:22:05 -03:00
parent 333615d051
commit 56094ef0f0
2 changed files with 6 additions and 8 deletions

View File

@ -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

View File

@ -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__
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))
status = HTTPStatus.error
else:
status = res.status
counter[status] += 1
return counter