basic flags example with async/await

This commit is contained in:
Luciano Ramalho 2015-07-13 23:19:39 -03:00
parent a16bd835d2
commit 22cfc8d6dd
2 changed files with 7 additions and 7 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.sublime-project
*.sublime-workspace
concurrency/flags/img/*.gif concurrency/flags/img/*.gif
concurrency/charfinder/charfinder_index.pickle concurrency/charfinder/charfinder_index.pickle
18-asyncio/charfinder/charfinder_index.pickle 18-asyncio/charfinder/charfinder_index.pickle

View File

@ -17,17 +17,15 @@ import aiohttp # <1>
from flags import BASE_URL, save_flag, show, main # <2> from flags import BASE_URL, save_flag, show, main # <2>
@asyncio.coroutine # <3> async def get_flag(cc): # <3>
def get_flag(cc):
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) # <4> resp = await aiohttp.request('GET', url) # <4>
image = yield from resp.read() # <5> image = await resp.read() # <5>
return image return image
@asyncio.coroutine async def download_one(cc): # <6>
def download_one(cc): # <6> image = await get_flag(cc) # <7>
image = yield from get_flag(cc) # <7>
show(cc) show(cc)
save_flag(image, cc.lower() + '.gif') save_flag(image, cc.lower() + '.gif')
return cc return cc