Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3dbd22a

Browse files
committed
flags2_await.py with async/await
1 parent31e2c4f commit3dbd22a

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

‎17-futures/countries/flags2_await.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# BEGIN FLAGS2_ASYNCIO_TOP
1919
importasyncio
2020
importcollections
21+
fromcontextlibimportclosing
2122

2223
importaiohttp
2324
fromaiohttpimportweb
@@ -36,26 +37,24 @@ def __init__(self, country_code):
3637
self.country_code=country_code
3738

3839

39-
@asyncio.coroutine
40-
defget_flag(base_url,cc):# <2>
40+
asyncdefget_flag(base_url,cc):# <2>
4141
url='{}/{cc}/{cc}.gif'.format(base_url,cc=cc.lower())
42-
resp=yieldfromaiohttp.request('GET',url)
43-
ifresp.status==200:
44-
image=yieldfromresp.read()
45-
returnimage
46-
elifresp.status==404:
47-
raiseweb.HTTPNotFound()
48-
else:
49-
raiseaiohttp.HttpProcessingError(
50-
code=resp.status,message=resp.reason,
51-
headers=resp.headers)
42+
withclosing(awaitaiohttp.request('GET',url))asresp:
43+
ifresp.status==200:
44+
image=awaitresp.read()
45+
returnimage
46+
elifresp.status==404:
47+
raiseweb.HTTPNotFound()
48+
else:
49+
raiseaiohttp.HttpProcessingError(
50+
code=resp.status,message=resp.reason,
51+
headers=resp.headers)
5252

5353

54-
@asyncio.coroutine
55-
defdownload_one(cc,base_url,semaphore,verbose):# <3>
54+
asyncdefdownload_one(cc,base_url,semaphore,verbose):# <3>
5655
try:
57-
with (yieldfromsemaphore):# <4>
58-
image=yieldfromget_flag(base_url,cc)# <5>
56+
with (awaitsemaphore):# <4>
57+
image=awaitget_flag(base_url,cc)# <5>
5958
exceptweb.HTTPNotFound:# <6>
6059
status=HTTPStatus.not_found
6160
msg='not found'
@@ -73,8 +72,7 @@ def download_one(cc, base_url, semaphore, verbose): # <3>
7372
# END FLAGS2_ASYNCIO_TOP
7473

7574
# BEGIN FLAGS2_ASYNCIO_DOWNLOAD_MANY
76-
@asyncio.coroutine
77-
defdownloader_coro(cc_list,base_url,verbose,concur_req):# <1>
75+
asyncdefdownloader_coro(cc_list,base_url,verbose,concur_req):# <1>
7876
counter=collections.Counter()
7977
semaphore=asyncio.Semaphore(concur_req)# <2>
8078
to_do= [download_one(cc,base_url,semaphore,verbose)
@@ -85,7 +83,7 @@ def downloader_coro(cc_list, base_url, verbose, concur_req): # <1>
8583
to_do_iter=tqdm.tqdm(to_do_iter,total=len(cc_list))# <5>
8684
forfutureinto_do_iter:# <6>
8785
try:
88-
res=yieldfromfuture# <7>
86+
res=awaitfuture# <7>
8987
exceptFetchErrorasexc:# <8>
9088
country_code=exc.country_code# <9>
9189
try:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp