update from Atlas

This commit is contained in:
Luciano Ramalho
2015-02-27 20:34:12 -03:00
parent 304d628066
commit 39e87de5cd
25 changed files with 2097 additions and 46 deletions

View File

@@ -0,0 +1,42 @@
"""Download flags of top 20 countries by population
ThreadPool version
Sample run::
$ python3 flags_threadpool.py
BD retrieved.
EG retrieved.
CN retrieved.
...
PH retrieved.
US retrieved.
IR retrieved.
20 flags downloaded in 0.93s
"""
from concurrent import futures
from flags import save_flag, get_flag, main
MAX_WORKERS = 100
def download_one(cc):
image = get_flag(cc)
print('{} retrieved.'.format(cc.upper()))
save_flag(image, cc.lower() + '.gif')
return cc
def download_many(cc_list):
workers = min(len(cc_list), MAX_WORKERS)
with futures.ThreadPoolExecutor(workers) as executor:
res = executor.map(download_one, sorted(cc_list))
return len(list(res))
if __name__ == '__main__':
main(download_many)