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

Appearance settings

Commit 423b10c

Browse filesBrowse files
committed
Merge branch 'master' of github.com:fluentpython/example-code-2e
2 parents 4fad21e + 4f4f759 commit 423b10c
Copy full SHA for 423b10c

File tree

Expand file treeCollapse file tree

3 files changed

+67
-55
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+67
-55
lines changed

‎18-with-match/lispy/py3.10/lis.py

Copy file name to clipboardExpand all lines: 18-with-match/lispy/py3.10/lis.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def change(self, key: Symbol, value: object) -> None:
6969
"Find where key is defined and change the value there."
7070
for map in self.maps:
7171
if key in map:
72-
map[key] = value
72+
map[key] = value # type: ignore[index]
7373
return
7474
raise KeyError(key)
7575
# end::ENV_CLASS[]

‎19-concurrency/primes/procs.py

Copy file name to clipboardExpand all lines: 19-concurrency/primes/procs.py
+29-25Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,44 @@ def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
3333
results.put(PrimeResult(0, False, 0.0)) # <10>
3434
# end::PRIMES_PROC_TOP[]
3535

36-
# tag::PRIMES_PROC_MAIN[]
37-
def main() -> None:
38-
if len(sys.argv) < 2: # <1>
39-
workers = cpu_count()
40-
else:
41-
workers = int(sys.argv[1])
42-
43-
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
44-
45-
jobs: JobQueue = SimpleQueue() # <2>
46-
results: ResultQueue = SimpleQueue()
47-
t0 = perf_counter()
48-
49-
for n in NUMBERS: # <3>
50-
jobs.put(n)
51-
36+
# tag::PRIMES_PROC_MIDDLE[]
37+
def start_jobs(workers: int, jobs: JobQueue, results: ResultQueue) -> None:
38+
for n in NUMBERS:
39+
jobs.put(n) # <1>
5240
for _ in range(workers):
53-
proc = Process(target=worker, args=(jobs, results)) # <4>
54-
proc.start() # <5>
55-
jobs.put(0) # <6>
41+
proc = Process(target=worker, args=(jobs, results)) # <2>
42+
proc.start() # <3>
43+
jobs.put(0) # <4>
5644

57-
workers_done = 0
45+
def report(workers: int, results: ResultQueue) -> int:
5846
checked = 0
59-
while workers_done < workers: # <7>
60-
n, prime, elapsed = results.get() # <8>
47+
workers_done = 0
48+
while workers_done < workers:
49+
n, prime, elapsed = results.get()
6150
if n == 0:
62-
workers_done += 1 # <9>
51+
workers_done += 1
6352
else:
6453
checked += 1
6554
label = 'P' if prime else ' '
66-
print(f'{n:16} {label} {elapsed:9.6f}s') # <10>
55+
print(f'{n:16} {label} {elapsed:9.6f}s')
56+
return checked
57+
# end::PRIMES_PROC_MIDDLE[]
6758

59+
# tag::PRIMES_PROC_MAIN[]
60+
def main() -> None:
61+
if len(sys.argv) < 2:
62+
workers = cpu_count()
63+
else:
64+
workers = int(sys.argv[1])
65+
66+
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
67+
t0 = perf_counter()
68+
jobs: JobQueue = SimpleQueue()
69+
results: ResultQueue = SimpleQueue()
70+
start_jobs(workers, jobs, results)
71+
checked = report(workers, results)
6872
elapsed = perf_counter() - t0
69-
print(f'{checked} checks in {elapsed:.2f}s') # <11>
73+
print(f'{checked} checks in {elapsed:.2f}s')
7074

7175
if __name__ == '__main__':
7276
main()

‎19-concurrency/primes/threads.py

Copy file name to clipboard
+37-29Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22

33
"""
4-
threads.py: shows that Python threads are slower than
5-
sequential code for CPU-intensive work.
4+
threads.py: shows that Python threads are slower
5+
than sequential code for CPU-intensive work.
66
"""
77

88
import os
@@ -14,52 +14,60 @@
1414

1515
from primes import is_prime, NUMBERS
1616

17-
class PrimeResult(NamedTuple): # <3>
17+
class PrimeResult(NamedTuple):
1818
n: int
1919
prime: bool
2020
elapsed: float
2121

22-
JobQueue = SimpleQueue[int]
23-
ResultQueue = SimpleQueue[PrimeResult]
22+
JobQueue = SimpleQueue[int] # <4>
23+
ResultQueue = SimpleQueue[PrimeResult] # <5>
2424

25-
def check(n: int) -> PrimeResult:
25+
def check(n: int) -> PrimeResult: # <6>
2626
t0 = perf_counter()
2727
res = is_prime(n)
2828
return PrimeResult(n, res, perf_counter() - t0)
2929

30-
def worker(jobs: JobQueue, results: ResultQueue) -> None:
31-
while n := jobs.get():
32-
results.put(check(n))
33-
34-
def main() -> None:
35-
if len(sys.argv) < 2: # <1>
36-
workers = os.cpu_count() or 1 # make mypy happy
37-
else:
38-
workers = int(sys.argv[1])
39-
40-
print(f'Checking {len(NUMBERS)} numbers with {workers} threads:')
41-
42-
jobs: JobQueue = SimpleQueue() # <2>
43-
results: ResultQueue = SimpleQueue()
44-
t0 = perf_counter()
30+
def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
31+
while n := jobs.get(): # <8>
32+
results.put(check(n)) # <9>
33+
results.put(PrimeResult(0, False, 0.0))
4534

35+
def start_jobs(workers: int, jobs: JobQueue, results: ResultQueue) -> None:
4636
for n in NUMBERS: # <3>
4737
jobs.put(n)
48-
4938
for _ in range(workers):
5039
proc = Thread(target=worker, args=(jobs, results)) # <4>
5140
proc.start() # <5>
5241
jobs.put(0) # <6>
5342

54-
while True:
55-
n, prime, elapsed = results.get() # <7>
56-
label = 'P' if prime else ' '
57-
print(f'{n:16} {label} {elapsed:9.6f}s')
58-
if jobs.empty(): # <8>
59-
break
43+
def report(workers: int, results: ResultQueue) -> int:
44+
checked = 0
45+
workers_done = 0
46+
while workers_done < workers:
47+
n, prime, elapsed = results.get()
48+
if n == 0:
49+
workers_done += 1
50+
else:
51+
checked += 1
52+
label = 'P' if prime else ' '
53+
print(f'{n:16} {label} {elapsed:9.6f}s')
54+
return checked
55+
56+
def main() -> None:
57+
if len(sys.argv) < 2:
58+
workers = os.cpu_count()
59+
else:
60+
workers = int(sys.argv[1])
6061

62+
print(f'Checking {len(NUMBERS)} numbers with {workers} threads:')
63+
t0 = perf_counter()
64+
jobs: JobQueue = SimpleQueue()
65+
results: ResultQueue = SimpleQueue()
66+
start_jobs(workers, jobs, results)
67+
checked = report(workers, results)
6168
elapsed = perf_counter() - t0
62-
print(f'Total time: {elapsed:.2f}s')
69+
print(f'{checked} checks in {elapsed:.2f}s')
6370

6471
if __name__ == '__main__':
6572
main()
73+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.