Skip to content

Commit a3bf91b

Browse files
committed
ch20 files
1 parent 07856d4 commit a3bf91b

File tree

9 files changed

+945
-28
lines changed

9 files changed

+945
-28
lines changed
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
1 42.05
2+
2 22.81
3+
3 15.95
4+
4 13.28
5+
5 12.27
6+
6 10.38
7+
7 11.00
8+
8 11.41
9+
9 12.17
10+
10 12.69
11+
11 11.34
12+
12 11.35
13+
13 10.88
14+
14 11.63
15+
15 11.79
16+
16 11.49
17+
17 11.29
18+
18 10.78
19+
19 10.92
20+
20 11.07
21+
1 40.47
22+
2 22.93
23+
3 15.88
24+
4 13.25
25+
5 12.22
26+
6 10.95
27+
7 10.81
28+
8 11.26
29+
9 11.96
30+
10 12.43
31+
11 11.19
32+
12 10.91
33+
13 11.03
34+
14 10.56
35+
15 10.62
36+
16 11.73
37+
17 11.00
38+
18 12.81
39+
19 12.38
40+
20 11.28
41+
1 40.81
42+
2 22.79
43+
3 15.83
44+
4 13.27
45+
5 12.33
46+
6 10.65
47+
7 10.94
48+
8 11.48
49+
9 11.78
50+
10 12.78
51+
11 11.31
52+
12 10.97
53+
13 10.80
54+
14 10.93
55+
15 10.69
56+
16 10.57
57+
17 10.54
58+
18 10.55
59+
19 10.79
60+
20 10.64
61+
1 40.85
62+
2 22.67
63+
3 16.05
64+
4 13.21
65+
5 12.53
66+
6 10.84
67+
7 10.80
68+
8 11.31
69+
9 11.69
70+
10 12.51
71+
11 11.22
72+
12 11.35
73+
13 11.00
74+
14 10.64
75+
15 10.89
76+
16 10.49
77+
17 10.55
78+
18 10.76
79+
19 10.54
80+
20 10.75
81+
1 40.41
82+
2 22.75
83+
3 15.87
84+
4 13.19
85+
5 12.33
86+
6 10.50
87+
7 10.84
88+
8 11.55
89+
9 11.79
90+
10 12.53
91+
11 11.24
92+
12 11.13
93+
13 10.89
94+
14 10.52
95+
15 10.74
96+
16 10.68
97+
17 10.88
98+
18 10.61
99+
19 11.07
100+
20 10.71
101+
1 40.45
102+
2 22.73
103+
3 16.35
104+
4 13.09
105+
5 12.19
106+
6 10.39
107+
7 11.01
108+
8 11.30
109+
9 11.81
110+
10 12.24
111+
11 11.21
112+
12 11.13
113+
13 10.66
114+
14 10.56
115+
15 10.91
116+
16 10.49
117+
17 10.58
118+
18 10.61
119+
19 10.60
120+
20 10.69
121+
1 40.36
122+
2 22.64
123+
3 15.95
124+
4 13.20
125+
5 12.27
126+
6 10.34
127+
7 10.47
128+
8 11.34
129+
9 11.68
130+
10 12.30
131+
11 11.04
132+
12 10.85
133+
13 10.78
134+
14 10.64
135+
15 10.63
136+
16 10.58
137+
17 10.67
138+
18 10.64
139+
19 10.71
140+
20 10.68
141+
1 40.70
142+
2 22.71
143+
3 15.81
144+
4 13.10
145+
5 12.29
146+
6 10.34
147+
7 10.40
148+
8 11.40
149+
9 11.71
150+
10 12.56
151+
11 11.29
152+
12 10.99
153+
13 10.51
154+
14 10.69
155+
15 10.53
156+
16 11.04
157+
17 10.67
158+
18 10.71
159+
19 10.86
160+
20 10.78
161+
1 40.69
162+
2 22.80
163+
3 15.88
164+
4 13.13
165+
5 12.19
166+
6 10.36
167+
7 10.77
168+
8 11.32
169+
9 11.66
170+
10 12.29
171+
11 11.06
172+
12 10.89
173+
13 10.66
174+
14 10.61
175+
15 10.36
176+
16 10.57
177+
17 10.94
178+
18 10.57
179+
19 10.81
180+
20 10.72
181+
1 40.81
182+
2 22.76
183+
3 15.84
184+
4 13.10
185+
5 12.25
186+
6 10.33
187+
7 10.58
188+
8 11.51
189+
9 11.69
190+
10 12.45
191+
11 11.51
192+
12 11.53
193+
13 10.61
194+
14 10.52
195+
15 10.57
196+
16 10.57
197+
17 10.76
198+
18 10.60
199+
19 10.66
200+
20 10.73
201+
1 40.84
202+
2 22.83
203+
3 15.86
204+
4 13.27
205+
5 12.39
206+
6 10.45
207+
7 10.87
208+
8 11.42
209+
9 11.70
210+
10 12.55
211+
11 11.43
212+
12 10.98
213+
13 10.81
214+
14 10.69
215+
15 10.68
216+
16 10.71
217+
17 10.80
218+
18 10.76
219+
19 10.90
220+
20 11.02
221+
1 40.99
222+
2 22.99
223+
3 16.10
224+
4 13.46
225+
5 12.45
226+
6 10.47
227+
7 10.79
228+
8 11.74
229+
9 11.64
230+
10 12.37
231+
11 11.20
232+
12 11.09
233+
13 10.82
234+
14 10.61
235+
15 10.56
236+
16 10.53
237+
17 10.60
238+
18 10.81
239+
19 10.72
240+
20 10.62
241+
1 40.94
242+
2 23.09
243+
3 16.03
244+
4 13.40
245+
5 12.51
246+
6 10.38
247+
7 10.58
248+
8 11.37
249+
9 11.75
250+
10 12.87
251+
11 12.11
252+
12 11.37
253+
13 11.84
254+
14 11.30
255+
15 11.29
256+
16 11.36
257+
17 11.01
258+
18 11.37
259+
19 11.07
260+
20 10.94

20-concurrency/primes/procs.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,57 @@
11
# tag::PRIMES_PROC_TOP[]
22
from time import perf_counter
3-
from typing import Tuple, List, NamedTuple
4-
from multiprocessing import Process, SimpleQueue # <1>
3+
from typing import Tuple, NamedTuple
4+
from multiprocessing import Process, SimpleQueue, cpu_count # <1>
55
from multiprocessing import queues # <2>
6+
import sys
67

78
from primes import is_prime, NUMBERS
89

910
class Result(NamedTuple): # <3>
1011
flag: bool
1112
elapsed: float
1213

13-
JobQueue = queues.SimpleQueue[Tuple[int, Result]] # <4>
14+
JobQueue = queues.SimpleQueue[int] # <4>
15+
ResultQueue = queues.SimpleQueue[Tuple[int, Result]] # <5>
1416

15-
def check(n: int) -> Result: # <5>
17+
def check(n: int) -> Result: # <6>
1618
t0 = perf_counter()
1719
res = is_prime(n)
1820
return Result(res, perf_counter() - t0)
1921

20-
def job(n: int, results: JobQueue) -> None: # <6>
21-
results.put((n, check(n))) # <7>
22+
def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
23+
while n := jobs.get(): # <8>
24+
result = check(n) # <9>
25+
results.put((n, result)) # <10>
2226
# end::PRIMES_PROC_TOP[]
2327

2428
# tag::PRIMES_PROC_MAIN[]
2529
def main() -> None:
30+
if len(sys.argv) < 2: # <1>
31+
workers = cpu_count()
32+
else:
33+
workers = int(sys.argv[1])
34+
2635
t0 = perf_counter()
27-
results: JobQueue = SimpleQueue() # <1>
28-
workers: List[Process] = [] # <2>
36+
jobs: JobQueue = SimpleQueue() # <2>
37+
results: ResultQueue = SimpleQueue()
38+
39+
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
2940

30-
for n in NUMBERS:
31-
worker = Process(target=job, args=(n, results)) # <3>
32-
worker.start() # <4>
33-
workers.append(worker) # <5>
41+
for n in NUMBERS: # <3>
42+
jobs.put(n)
3443

35-
for _ in workers: # <6>
44+
for _ in range(workers):
45+
proc = Process(target=worker, args=(jobs, results)) # <4>
46+
proc.start() # <5>
47+
jobs.put(0) # <6>
48+
49+
while True:
3650
n, (prime, elapsed) = results.get() # <7>
3751
label = 'P' if prime else ' '
38-
print(f'{n:16} {label} {elapsed:9.6f}s')
39-
52+
print(f'{n:16} {label} {elapsed:9.6f}s') # <8>
53+
if jobs.empty(): # <9>
54+
break
4055

4156
time = perf_counter() - t0
4257
print('Total time:', f'{time:0.2f}s')

20-concurrency/primes/run_procs.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#/bin/bash
2+
for i in {1..20}; do echo -n $i; python3 procs.py $i | tail -1; done
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# spinner_async_experiment.py
2+
3+
# credits: Example by Luciano Ramalho inspired by
4+
# Michele Simionato's multiprocessing example in the python-list:
5+
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
6+
7+
import asyncio
8+
import itertools
9+
10+
import primes
11+
12+
async def spin(msg: str) -> None:
13+
for char in itertools.cycle(r'\|/-'):
14+
status = f'\r{char} {msg}'
15+
print(status, flush=True, end='')
16+
try:
17+
await asyncio.sleep(.1)
18+
except asyncio.CancelledError:
19+
break
20+
print('THIS WILL NEVER BE OUTPUT')
21+
22+
async def check(n: int) -> int:
23+
return primes.is_prime(n) # <4>
24+
25+
async def supervisor(n: int) -> int:
26+
spinner = asyncio.create_task(spin('thinking!')) # <1>
27+
print('spinner object:', spinner) # <2>
28+
result = await check(n) # <3>
29+
spinner.cancel() # <5>
30+
return result
31+
# end::SPINNER_ASYNC_EXPERIMENT[]
32+
33+
def main() -> None:
34+
n = 5_000_111_000_222_021
35+
result = asyncio.run(supervisor(n))
36+
msg = 'is' if result else 'is not'
37+
print(f'{n:,} {msg} prime')
38+
39+
if __name__ == '__main__':
40+
main()

0 commit comments

Comments
 (0)