|
18 | 18 | # BEGIN FLAGS2_ASYNCIO_TOP |
19 | 19 | import asyncio |
20 | 20 | import collections |
| 21 | +from contextlib import closing |
21 | 22 |
|
22 | 23 | import aiohttp |
23 | 24 | from aiohttp import web |
@@ -36,26 +37,24 @@ def __init__(self, country_code): |
36 | 37 | self.country_code = country_code |
37 | 38 |
|
38 | 39 |
|
39 | | -@asyncio.coroutine |
40 | | -def get_flag(base_url, cc): # <2> |
| 40 | +async def get_flag(base_url, cc): # <2> |
41 | 41 | url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower()) |
42 | | - resp = yield from aiohttp.request('GET', url) |
43 | | - if resp.status == 200: |
44 | | - image = yield from resp.read() |
45 | | - return image |
46 | | - elif resp.status == 404: |
47 | | - raise web.HTTPNotFound() |
48 | | - else: |
49 | | - raise aiohttp.HttpProcessingError( |
50 | | - code=resp.status, message=resp.reason, |
51 | | - headers=resp.headers) |
| 42 | + with closing(await aiohttp.request('GET', url)) as resp: |
| 43 | + if resp.status == 200: |
| 44 | + image = await resp.read() |
| 45 | + return image |
| 46 | + elif resp.status == 404: |
| 47 | + raise web.HTTPNotFound() |
| 48 | + else: |
| 49 | + raise aiohttp.HttpProcessingError( |
| 50 | + code=resp.status, message=resp.reason, |
| 51 | + headers=resp.headers) |
52 | 52 |
|
53 | 53 |
|
54 | | -@asyncio.coroutine |
55 | | -def download_one(cc, base_url, semaphore, verbose): # <3> |
| 54 | +async def download_one(cc, base_url, semaphore, verbose): # <3> |
56 | 55 | try: |
57 | | - with (yield from semaphore): # <4> |
58 | | - image = yield from get_flag(base_url, cc) # <5> |
| 56 | + with (await semaphore): # <4> |
| 57 | + image = await get_flag(base_url, cc) # <5> |
59 | 58 | except web.HTTPNotFound: # <6> |
60 | 59 | status = HTTPStatus.not_found |
61 | 60 | msg = 'not found' |
@@ -73,8 +72,7 @@ def download_one(cc, base_url, semaphore, verbose): # <3> |
73 | 72 | # END FLAGS2_ASYNCIO_TOP |
74 | 73 |
|
75 | 74 | # BEGIN FLAGS2_ASYNCIO_DOWNLOAD_MANY |
76 | | -@asyncio.coroutine |
77 | | -def downloader_coro(cc_list, base_url, verbose, concur_req): # <1> |
| 75 | +async def downloader_coro(cc_list, base_url, verbose, concur_req): # <1> |
78 | 76 | counter = collections.Counter() |
79 | 77 | semaphore = asyncio.Semaphore(concur_req) # <2> |
80 | 78 | to_do = [download_one(cc, base_url, semaphore, verbose) |
@@ -85,7 +83,7 @@ def downloader_coro(cc_list, base_url, verbose, concur_req): # <1> |
85 | 83 | to_do_iter = tqdm.tqdm(to_do_iter, total=len(cc_list)) # <5> |
86 | 84 | for future in to_do_iter: # <6> |
87 | 85 | try: |
88 | | - res = yield from future # <7> |
| 86 | + res = await future # <7> |
89 | 87 | except FetchError as exc: # <8> |
90 | 88 | country_code = exc.country_code # <9> |
91 | 89 | try: |
|