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
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commiteeb6b3d

Browse files
committed
some chapter 17 examples updated for Python 3.7
1 parent3dd1174 commiteeb6b3d

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

‎.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.vscode/
2+
.venv3?/
13
*.sublime-project
24
*.sublime-workspace
35
.env*

‎17-futures-py3.7/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Updated sample code for Chapter 17 - "Concurrency with futures"
2+
3+
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
4+
http://shop.oreilly.com/product/0636920032519.do
5+
6+
This directory contains code updated to run with Python 3.7 and
7+
**aiohttp** 3.5. When the first edition of "Fluent Python" was
8+
written, the **asyncio** package was provisional, and the latest
9+
version of **aiohttp** was 0.13.1. The API for both packages had
10+
significant breaking changes.

‎17-futures-py3.7/countries/flags.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Download flags of top 20 countries by population
2+
3+
Sequential version
4+
5+
Sample run::
6+
7+
$ python3 flags.py
8+
BD BR CD CN DE EG ET FR ID IN IR JP MX NG PH PK RU TR US VN
9+
20 flags downloaded in 5.49s
10+
11+
"""
12+
# BEGIN FLAGS_PY
13+
importos
14+
importtime
15+
importsys
16+
17+
importrequests# <1>
18+
19+
POP20_CC= ('CN IN US ID BR PK NG BD RU JP '
20+
'MX PH VN ET EG DE IR TR CD FR').split()# <2>
21+
22+
BASE_URL='http://flupy.org/data/flags'# <3>
23+
24+
DEST_DIR='downloads/'# <4>
25+
26+
27+
defsave_flag(img,filename):# <5>
28+
path=os.path.join(DEST_DIR,filename)
29+
withopen(path,'wb')asfp:
30+
fp.write(img)
31+
32+
33+
defget_flag(cc):# <6>
34+
url='{}/{cc}/{cc}.gif'.format(BASE_URL,cc=cc.lower())
35+
resp=requests.get(url)
36+
returnresp.content
37+
38+
39+
defshow(text):# <7>
40+
print(text,end=' ')
41+
sys.stdout.flush()
42+
43+
44+
defdownload_many(cc_list):# <8>
45+
forccinsorted(cc_list):# <9>
46+
image=get_flag(cc)
47+
show(cc)
48+
save_flag(image,cc.lower()+'.gif')
49+
50+
returnlen(cc_list)
51+
52+
53+
defmain():# <10>
54+
t0=time.time()
55+
count=download_many(POP20_CC)
56+
elapsed=time.time()-t0
57+
msg='\n{} flags downloaded in {:.2f}s'
58+
print(msg.format(count,elapsed))
59+
60+
61+
if__name__=='__main__':
62+
main()
63+
# END FLAGS_PY
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Download flags of top 20 countries by population
2+
3+
asyncio + aiottp version
4+
5+
Sample run::
6+
7+
$ python3 flags_asyncio.py
8+
CN EG BR IN ID RU NG VN JP DE TR PK FR ET MX PH US IR CD BD
9+
20 flags downloaded in 0.35s
10+
11+
"""
12+
# BEGIN FLAGS_ASYNCIO
13+
importos
14+
importtime
15+
importsys
16+
importasyncio# <1>
17+
18+
importaiohttp# <2>
19+
20+
21+
POP20_CC= ('CN IN US ID BR PK NG BD RU JP '
22+
'MX PH VN ET EG DE IR TR CD FR').split()
23+
24+
BASE_URL='http://flupy.org/data/flags'
25+
26+
DEST_DIR='downloads/'
27+
28+
29+
defsave_flag(img,filename):
30+
path=os.path.join(DEST_DIR,filename)
31+
withopen(path,'wb')asfp:
32+
fp.write(img)
33+
34+
35+
asyncdefget_flag(session,cc):# <3>
36+
url='{}/{cc}/{cc}.gif'.format(BASE_URL,cc=cc.lower())
37+
asyncwithsession.get(url)asresp:# <4>
38+
returnawaitresp.read()# <5>
39+
40+
41+
defshow(text):
42+
print(text,end=' ')
43+
sys.stdout.flush()
44+
45+
46+
asyncdefdownload_one(session,cc):# <6>
47+
image=awaitget_flag(session,cc)# <7>
48+
show(cc)
49+
save_flag(image,cc.lower()+'.gif')
50+
returncc
51+
52+
53+
asyncdefdownload_many(cc_list):
54+
asyncwithaiohttp.ClientSession()assession:# <8>
55+
res=awaitasyncio.gather(# <9>
56+
*[asyncio.create_task(download_one(session,cc))
57+
forccinsorted(cc_list)])
58+
59+
returnlen(res)
60+
61+
62+
defmain():# <10>
63+
t0=time.time()
64+
count=asyncio.run(download_many(POP20_CC))
65+
elapsed=time.time()-t0
66+
msg='\n{} flags downloaded in {:.2f}s'
67+
print(msg.format(count,elapsed))
68+
69+
70+
if__name__=='__main__':
71+
main()
72+
# END FLAGS_ASYNCIO
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Download flags of top 20 countries by population
2+
3+
ThreadPoolExecutor version
4+
5+
Sample run::
6+
7+
$ python3 flags_threadpool.py
8+
DE FR BD CN EG RU IN TR VN ID JP BR NG MX PK ET PH CD US IR
9+
20 flags downloaded in 0.35s
10+
11+
"""
12+
# BEGIN FLAGS_THREADPOOL
13+
importos
14+
importtime
15+
importsys
16+
fromconcurrentimportfutures# <1>
17+
18+
importrequests
19+
20+
POP20_CC= ('CN IN US ID BR PK NG BD RU JP '
21+
'MX PH VN ET EG DE IR TR CD FR').split()
22+
23+
BASE_URL='http://flupy.org/data/flags'
24+
25+
DEST_DIR='downloads/'
26+
27+
MAX_WORKERS=20# <2>
28+
29+
defsave_flag(img,filename):
30+
path=os.path.join(DEST_DIR,filename)
31+
withopen(path,'wb')asfp:
32+
fp.write(img)
33+
34+
35+
defget_flag(cc):
36+
url='{}/{cc}/{cc}.gif'.format(BASE_URL,cc=cc.lower())
37+
resp=requests.get(url)
38+
returnresp.content
39+
40+
41+
defshow(text):
42+
print(text,end=' ')
43+
sys.stdout.flush()
44+
45+
46+
defdownload_one(cc):# <3>
47+
image=get_flag(cc)
48+
show(cc)
49+
save_flag(image,cc.lower()+'.gif')
50+
returncc
51+
52+
53+
defdownload_many(cc_list):
54+
workers=min(MAX_WORKERS,len(cc_list))# <4>
55+
withfutures.ThreadPoolExecutor(workers)asexecutor:# <5>
56+
res=executor.map(download_one,sorted(cc_list))# <6>
57+
58+
returnlen(list(res))# <7>
59+
60+
61+
defmain():# <10>
62+
t0=time.time()
63+
count=download_many(POP20_CC)
64+
elapsed=time.time()-t0
65+
msg='\n{} flags downloaded in {:.2f}s'
66+
print(msg.format(count,elapsed))
67+
68+
69+
if__name__=='__main__':
70+
main()
71+
# END FLAGS_THREADPOOL
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests==2.21.0
2+
aiohttp==3.5.4

‎17-futures-py3.7/demo_executor_map.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Experiment with ``ThreadPoolExecutor.map``
3+
"""
4+
# BEGIN EXECUTOR_MAP
5+
fromtimeimportsleep,strftime
6+
fromconcurrentimportfutures
7+
8+
9+
defdisplay(*args):# <1>
10+
print(strftime('[%H:%M:%S]'),end=' ')
11+
print(*args)
12+
13+
14+
defloiter(n):# <2>
15+
msg='{}loiter({}): doing nothing for {}s...'
16+
display(msg.format('\t'*n,n,n))
17+
sleep(n)
18+
msg='{}loiter({}): done.'
19+
display(msg.format('\t'*n,n))
20+
returnn*10# <3>
21+
22+
23+
defmain():
24+
display('Script starting.')
25+
executor=futures.ThreadPoolExecutor(max_workers=3)# <4>
26+
results=executor.map(loiter,range(5))# <5>
27+
display('results:',results)# <6>.
28+
display('Waiting for individual results:')
29+
fori,resultinenumerate(results):# <7>
30+
display('result {}: {}'.format(i,result))
31+
32+
33+
main()
34+
# END EXECUTOR_MAP

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp