flags2_await.py with async/await

This commit is contained in:
Luciano Ramalho
2015-07-13 23:51:34 -03:00
parent 31e2c4fe4e
commit 3dbd22af09

View File

@@ -18,6 +18,7 @@ Sample run::
# BEGIN FLAGS2_ASYNCIO_TOP # BEGIN FLAGS2_ASYNCIO_TOP
import asyncio import asyncio
import collections import collections
from contextlib import closing
import aiohttp import aiohttp
from aiohttp import web from aiohttp import web
@@ -36,26 +37,24 @@ class FetchError(Exception): # <1>
self.country_code = country_code self.country_code = country_code
@asyncio.coroutine async def get_flag(base_url, cc): # <2>
def get_flag(base_url, cc): # <2>
url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower()) url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
resp = yield from aiohttp.request('GET', url) with closing(await aiohttp.request('GET', url)) as resp:
if resp.status == 200: if resp.status == 200:
image = yield from resp.read() image = await resp.read()
return image return image
elif resp.status == 404: elif resp.status == 404:
raise web.HTTPNotFound() raise web.HTTPNotFound()
else: else:
raise aiohttp.HttpProcessingError( raise aiohttp.HttpProcessingError(
code=resp.status, message=resp.reason, code=resp.status, message=resp.reason,
headers=resp.headers) headers=resp.headers)
@asyncio.coroutine async def download_one(cc, base_url, semaphore, verbose): # <3>
def download_one(cc, base_url, semaphore, verbose): # <3>
try: try:
with (yield from semaphore): # <4> with (await semaphore): # <4>
image = yield from get_flag(base_url, cc) # <5> image = await get_flag(base_url, cc) # <5>
except web.HTTPNotFound: # <6> except web.HTTPNotFound: # <6>
status = HTTPStatus.not_found status = HTTPStatus.not_found
msg = 'not found' msg = 'not found'
@@ -73,8 +72,7 @@ def download_one(cc, base_url, semaphore, verbose): # <3>
# END FLAGS2_ASYNCIO_TOP # END FLAGS2_ASYNCIO_TOP
# BEGIN FLAGS2_ASYNCIO_DOWNLOAD_MANY # BEGIN FLAGS2_ASYNCIO_DOWNLOAD_MANY
@asyncio.coroutine async def downloader_coro(cc_list, base_url, verbose, concur_req): # <1>
def downloader_coro(cc_list, base_url, verbose, concur_req): # <1>
counter = collections.Counter() counter = collections.Counter()
semaphore = asyncio.Semaphore(concur_req) # <2> semaphore = asyncio.Semaphore(concur_req) # <2>
to_do = [download_one(cc, base_url, semaphore, verbose) to_do = [download_one(cc, base_url, semaphore, verbose)
@@ -85,7 +83,7 @@ def downloader_coro(cc_list, base_url, verbose, concur_req): # <1>
to_do_iter = tqdm.tqdm(to_do_iter, total=len(cc_list)) # <5> to_do_iter = tqdm.tqdm(to_do_iter, total=len(cc_list)) # <5>
for future in to_do_iter: # <6> for future in to_do_iter: # <6>
try: try:
res = yield from future # <7> res = await future # <7>
except FetchError as exc: # <8> except FetchError as exc: # <8>
country_code = exc.country_code # <9> country_code = exc.country_code # <9>
try: try: