renumbering chapters >= 19
This commit is contained in:
260
19-concurrency/primes/log-procs.txt
Normal file
260
19-concurrency/primes/log-procs.txt
Normal file
@@ -0,0 +1,260 @@
|
||||
1 42.05
|
||||
2 22.81
|
||||
3 15.95
|
||||
4 13.28
|
||||
5 12.27
|
||||
6 10.38
|
||||
7 11.00
|
||||
8 11.41
|
||||
9 12.17
|
||||
10 12.69
|
||||
11 11.34
|
||||
12 11.35
|
||||
13 10.88
|
||||
14 11.63
|
||||
15 11.79
|
||||
16 11.49
|
||||
17 11.29
|
||||
18 10.78
|
||||
19 10.92
|
||||
20 11.07
|
||||
1 40.47
|
||||
2 22.93
|
||||
3 15.88
|
||||
4 13.25
|
||||
5 12.22
|
||||
6 10.95
|
||||
7 10.81
|
||||
8 11.26
|
||||
9 11.96
|
||||
10 12.43
|
||||
11 11.19
|
||||
12 10.91
|
||||
13 11.03
|
||||
14 10.56
|
||||
15 10.62
|
||||
16 11.73
|
||||
17 11.00
|
||||
18 12.81
|
||||
19 12.38
|
||||
20 11.28
|
||||
1 40.81
|
||||
2 22.79
|
||||
3 15.83
|
||||
4 13.27
|
||||
5 12.33
|
||||
6 10.65
|
||||
7 10.94
|
||||
8 11.48
|
||||
9 11.78
|
||||
10 12.78
|
||||
11 11.31
|
||||
12 10.97
|
||||
13 10.80
|
||||
14 10.93
|
||||
15 10.69
|
||||
16 10.57
|
||||
17 10.54
|
||||
18 10.55
|
||||
19 10.79
|
||||
20 10.64
|
||||
1 40.85
|
||||
2 22.67
|
||||
3 16.05
|
||||
4 13.21
|
||||
5 12.53
|
||||
6 10.84
|
||||
7 10.80
|
||||
8 11.31
|
||||
9 11.69
|
||||
10 12.51
|
||||
11 11.22
|
||||
12 11.35
|
||||
13 11.00
|
||||
14 10.64
|
||||
15 10.89
|
||||
16 10.49
|
||||
17 10.55
|
||||
18 10.76
|
||||
19 10.54
|
||||
20 10.75
|
||||
1 40.41
|
||||
2 22.75
|
||||
3 15.87
|
||||
4 13.19
|
||||
5 12.33
|
||||
6 10.50
|
||||
7 10.84
|
||||
8 11.55
|
||||
9 11.79
|
||||
10 12.53
|
||||
11 11.24
|
||||
12 11.13
|
||||
13 10.89
|
||||
14 10.52
|
||||
15 10.74
|
||||
16 10.68
|
||||
17 10.88
|
||||
18 10.61
|
||||
19 11.07
|
||||
20 10.71
|
||||
1 40.45
|
||||
2 22.73
|
||||
3 16.35
|
||||
4 13.09
|
||||
5 12.19
|
||||
6 10.39
|
||||
7 11.01
|
||||
8 11.30
|
||||
9 11.81
|
||||
10 12.24
|
||||
11 11.21
|
||||
12 11.13
|
||||
13 10.66
|
||||
14 10.56
|
||||
15 10.91
|
||||
16 10.49
|
||||
17 10.58
|
||||
18 10.61
|
||||
19 10.60
|
||||
20 10.69
|
||||
1 40.36
|
||||
2 22.64
|
||||
3 15.95
|
||||
4 13.20
|
||||
5 12.27
|
||||
6 10.34
|
||||
7 10.47
|
||||
8 11.34
|
||||
9 11.68
|
||||
10 12.30
|
||||
11 11.04
|
||||
12 10.85
|
||||
13 10.78
|
||||
14 10.64
|
||||
15 10.63
|
||||
16 10.58
|
||||
17 10.67
|
||||
18 10.64
|
||||
19 10.71
|
||||
20 10.68
|
||||
1 40.70
|
||||
2 22.71
|
||||
3 15.81
|
||||
4 13.10
|
||||
5 12.29
|
||||
6 10.34
|
||||
7 10.40
|
||||
8 11.40
|
||||
9 11.71
|
||||
10 12.56
|
||||
11 11.29
|
||||
12 10.99
|
||||
13 10.51
|
||||
14 10.69
|
||||
15 10.53
|
||||
16 11.04
|
||||
17 10.67
|
||||
18 10.71
|
||||
19 10.86
|
||||
20 10.78
|
||||
1 40.69
|
||||
2 22.80
|
||||
3 15.88
|
||||
4 13.13
|
||||
5 12.19
|
||||
6 10.36
|
||||
7 10.77
|
||||
8 11.32
|
||||
9 11.66
|
||||
10 12.29
|
||||
11 11.06
|
||||
12 10.89
|
||||
13 10.66
|
||||
14 10.61
|
||||
15 10.36
|
||||
16 10.57
|
||||
17 10.94
|
||||
18 10.57
|
||||
19 10.81
|
||||
20 10.72
|
||||
1 40.81
|
||||
2 22.76
|
||||
3 15.84
|
||||
4 13.10
|
||||
5 12.25
|
||||
6 10.33
|
||||
7 10.58
|
||||
8 11.51
|
||||
9 11.69
|
||||
10 12.45
|
||||
11 11.51
|
||||
12 11.53
|
||||
13 10.61
|
||||
14 10.52
|
||||
15 10.57
|
||||
16 10.57
|
||||
17 10.76
|
||||
18 10.60
|
||||
19 10.66
|
||||
20 10.73
|
||||
1 40.84
|
||||
2 22.83
|
||||
3 15.86
|
||||
4 13.27
|
||||
5 12.39
|
||||
6 10.45
|
||||
7 10.87
|
||||
8 11.42
|
||||
9 11.70
|
||||
10 12.55
|
||||
11 11.43
|
||||
12 10.98
|
||||
13 10.81
|
||||
14 10.69
|
||||
15 10.68
|
||||
16 10.71
|
||||
17 10.80
|
||||
18 10.76
|
||||
19 10.90
|
||||
20 11.02
|
||||
1 40.99
|
||||
2 22.99
|
||||
3 16.10
|
||||
4 13.46
|
||||
5 12.45
|
||||
6 10.47
|
||||
7 10.79
|
||||
8 11.74
|
||||
9 11.64
|
||||
10 12.37
|
||||
11 11.20
|
||||
12 11.09
|
||||
13 10.82
|
||||
14 10.61
|
||||
15 10.56
|
||||
16 10.53
|
||||
17 10.60
|
||||
18 10.81
|
||||
19 10.72
|
||||
20 10.62
|
||||
1 40.94
|
||||
2 23.09
|
||||
3 16.03
|
||||
4 13.40
|
||||
5 12.51
|
||||
6 10.38
|
||||
7 10.58
|
||||
8 11.37
|
||||
9 11.75
|
||||
10 12.87
|
||||
11 12.11
|
||||
12 11.37
|
||||
13 11.84
|
||||
14 11.30
|
||||
15 11.29
|
||||
16 11.36
|
||||
17 11.01
|
||||
18 11.37
|
||||
19 11.07
|
||||
20 10.94
|
||||
51
19-concurrency/primes/primes.py
Executable file
51
19-concurrency/primes/primes.py
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
|
||||
PRIME_FIXTURE = [
|
||||
(2, True),
|
||||
(142702110479723, True),
|
||||
(299593572317531, True),
|
||||
(3333333333333301, True),
|
||||
(3333333333333333, False),
|
||||
(3333335652092209, False),
|
||||
(4444444444444423, True),
|
||||
(4444444444444444, False),
|
||||
(4444444488888889, False),
|
||||
(5555553133149889, False),
|
||||
(5555555555555503, True),
|
||||
(5555555555555555, False),
|
||||
(6666666666666666, False),
|
||||
(6666666666666719, True),
|
||||
(6666667141414921, False),
|
||||
(7777777536340681, False),
|
||||
(7777777777777753, True),
|
||||
(7777777777777777, False),
|
||||
(9999999999999917, True),
|
||||
(9999999999999999, False),
|
||||
]
|
||||
|
||||
NUMBERS = [n for n, _ in PRIME_FIXTURE]
|
||||
|
||||
# tag::IS_PRIME[]
|
||||
def is_prime(n: int) -> bool:
|
||||
if n < 2:
|
||||
return False
|
||||
if n == 2:
|
||||
return True
|
||||
if n % 2 == 0:
|
||||
return False
|
||||
|
||||
root = math.isqrt(n)
|
||||
for i in range(3, root + 1, 2):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
# end::IS_PRIME[]
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
for n, prime in PRIME_FIXTURE:
|
||||
prime_res = is_prime(n)
|
||||
assert prime_res == prime
|
||||
print(n, prime)
|
||||
68
19-concurrency/primes/procs.py
Normal file
68
19-concurrency/primes/procs.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
procs.py: shows that multiprocessing on a multicore machine
|
||||
can be faster than sequential code for CPU-intensive work.
|
||||
"""
|
||||
|
||||
# tag::PRIMES_PROC_TOP[]
|
||||
import sys
|
||||
from time import perf_counter
|
||||
from typing import NamedTuple
|
||||
from multiprocessing import Process, SimpleQueue, cpu_count # <1>
|
||||
from multiprocessing import queues # <2>
|
||||
|
||||
from primes import is_prime, NUMBERS
|
||||
|
||||
class PrimeResult(NamedTuple): # <3>
|
||||
n: int
|
||||
prime: bool
|
||||
elapsed: float
|
||||
|
||||
JobQueue = queues.SimpleQueue[int] # <4>
|
||||
ResultQueue = queues.SimpleQueue[PrimeResult] # <5>
|
||||
|
||||
def check(n: int) -> PrimeResult: # <6>
|
||||
t0 = perf_counter()
|
||||
res = is_prime(n)
|
||||
return PrimeResult(n, res, perf_counter() - t0)
|
||||
|
||||
def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
|
||||
while n := jobs.get(): # <8>
|
||||
results.put(check(n)) # <9>
|
||||
# end::PRIMES_PROC_TOP[]
|
||||
|
||||
# tag::PRIMES_PROC_MAIN[]
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2: # <1>
|
||||
workers = cpu_count()
|
||||
else:
|
||||
workers = int(sys.argv[1])
|
||||
|
||||
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
|
||||
|
||||
jobs: JobQueue = SimpleQueue() # <2>
|
||||
results: ResultQueue = SimpleQueue()
|
||||
t0 = perf_counter()
|
||||
|
||||
for n in NUMBERS: # <3>
|
||||
jobs.put(n)
|
||||
|
||||
for _ in range(workers):
|
||||
proc = Process(target=worker, args=(jobs, results)) # <4>
|
||||
proc.start() # <5>
|
||||
jobs.put(0) # <6>
|
||||
|
||||
while True:
|
||||
n, prime, elapsed = results.get() # <7>
|
||||
label = 'P' if prime else ' '
|
||||
print(f'{n:16} {label} {elapsed:9.6f}s') # <8>
|
||||
if jobs.empty(): # <9>
|
||||
break
|
||||
|
||||
elapsed = perf_counter() - t0
|
||||
print(f'Total time: {elapsed:.2f}s')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
# end::PRIMES_PROC_MAIN[]
|
||||
51
19-concurrency/primes/py36/primes.py
Executable file
51
19-concurrency/primes/py36/primes.py
Executable file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import math
|
||||
|
||||
PRIME_FIXTURE = [
|
||||
(2, True),
|
||||
(142702110479723, True),
|
||||
(299593572317531, True),
|
||||
(3333333333333301, True),
|
||||
(3333333333333333, False),
|
||||
(3333335652092209, False),
|
||||
(4444444444444423, True),
|
||||
(4444444444444444, False),
|
||||
(4444444488888889, False),
|
||||
(5555553133149889, False),
|
||||
(5555555555555503, True),
|
||||
(5555555555555555, False),
|
||||
(6666666666666666, False),
|
||||
(6666666666666719, True),
|
||||
(6666667141414921, False),
|
||||
(7777777536340681, False),
|
||||
(7777777777777753, True),
|
||||
(7777777777777777, False),
|
||||
(9999999999999917, True),
|
||||
(9999999999999999, False),
|
||||
]
|
||||
|
||||
NUMBERS = [n for n, _ in PRIME_FIXTURE]
|
||||
|
||||
# tag::IS_PRIME[]
|
||||
def is_prime(n: int) -> bool:
|
||||
if n < 2:
|
||||
return False
|
||||
if n == 2:
|
||||
return True
|
||||
if n % 2 == 0:
|
||||
return False
|
||||
|
||||
root = math.floor(math.sqrt(n))
|
||||
for i in range(3, root + 1, 2):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
# end::IS_PRIME[]
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
for n, prime in PRIME_FIXTURE:
|
||||
prime_res = is_prime(n)
|
||||
assert prime_res == prime
|
||||
print(n, prime)
|
||||
71
19-concurrency/primes/py36/procs.py
Executable file
71
19-concurrency/primes/py36/procs.py
Executable file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
procs.py: shows that multiprocessing on a multicore machine
|
||||
can be faster than sequential code for CPU-intensive work.
|
||||
"""
|
||||
|
||||
# tag::PRIMES_PROC_TOP[]
|
||||
import sys
|
||||
from time import perf_counter
|
||||
from typing import NamedTuple
|
||||
from multiprocessing import Process, SimpleQueue, cpu_count # <1>
|
||||
from multiprocessing import queues # <2>
|
||||
|
||||
from primes import is_prime, NUMBERS
|
||||
|
||||
class PrimeResult(NamedTuple): # <3>
|
||||
n: int
|
||||
prime: bool
|
||||
elapsed: float
|
||||
|
||||
JobQueue = queues.SimpleQueue # <4>
|
||||
ResultQueue = queues.SimpleQueue # <5>
|
||||
|
||||
def check(n: int) -> PrimeResult: # <6>
|
||||
t0 = perf_counter()
|
||||
res = is_prime(n)
|
||||
return PrimeResult(n, res, perf_counter() - t0)
|
||||
|
||||
def worker(jobs: JobQueue, results: ResultQueue) -> None: # <7>
|
||||
while True:
|
||||
n = jobs.get() # <8>
|
||||
if n == 0:
|
||||
break
|
||||
results.put(check(n)) # <9>
|
||||
# end::PRIMES_PROC_TOP[]
|
||||
|
||||
# tag::PRIMES_PROC_MAIN[]
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2: # <1>
|
||||
workers = cpu_count()
|
||||
else:
|
||||
workers = int(sys.argv[1])
|
||||
|
||||
print(f'Checking {len(NUMBERS)} numbers with {workers} processes:')
|
||||
|
||||
jobs: JobQueue = SimpleQueue() # <2>
|
||||
results: ResultQueue = SimpleQueue()
|
||||
t0 = perf_counter()
|
||||
|
||||
for n in NUMBERS: # <3>
|
||||
jobs.put(n)
|
||||
|
||||
for _ in range(workers):
|
||||
proc = Process(target=worker, args=(jobs, results)) # <4>
|
||||
proc.start() # <5>
|
||||
jobs.put(0) # <6>
|
||||
|
||||
while True:
|
||||
n, prime, elapsed = results.get() # <7>
|
||||
label = 'P' if prime else ' '
|
||||
print(f'{n:16} {label} {elapsed:9.6f}s') # <8>
|
||||
if jobs.empty(): # <9>
|
||||
break
|
||||
|
||||
elapsed = perf_counter() - t0
|
||||
print(f'Total time: {elapsed:.2f}s')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
# end::PRIMES_PROC_MAIN[]
|
||||
2
19-concurrency/primes/run_procs.sh
Executable file
2
19-concurrency/primes/run_procs.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#/bin/bash
|
||||
for i in {1..20}; do echo -n $i; python3 procs.py $i | tail -1; done
|
||||
34
19-concurrency/primes/sequential.py
Normal file
34
19-concurrency/primes/sequential.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
sequential.py: baseline for comparing sequential, multiprocessing,
|
||||
and threading code for CPU-intensive work.
|
||||
"""
|
||||
|
||||
from time import perf_counter
|
||||
from typing import NamedTuple
|
||||
|
||||
from primes import is_prime, NUMBERS
|
||||
|
||||
class Result(NamedTuple): # <1>
|
||||
prime: bool
|
||||
elapsed: float
|
||||
|
||||
def check(n: int) -> Result: # <2>
|
||||
t0 = perf_counter()
|
||||
prime = is_prime(n)
|
||||
return Result(prime, perf_counter() - t0)
|
||||
|
||||
def main() -> None:
|
||||
print(f'Checking {len(NUMBERS)} numbers sequentially:')
|
||||
t0 = perf_counter()
|
||||
for n in NUMBERS: # <3>
|
||||
prime, elapsed = check(n)
|
||||
label = 'P' if prime else ' '
|
||||
print(f'{n:16} {label} {elapsed:9.6f}s')
|
||||
|
||||
elapsed = perf_counter() - t0 # <4>
|
||||
print(f'Total time: {elapsed:.2f}s')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
40
19-concurrency/primes/spinner_prime_async_broken.py
Normal file
40
19-concurrency/primes/spinner_prime_async_broken.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# spinner_async_experiment.py
|
||||
|
||||
# credits: Example by Luciano Ramalho inspired by
|
||||
# Michele Simionato's multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||
|
||||
import asyncio
|
||||
import itertools
|
||||
|
||||
import primes
|
||||
|
||||
async def spin(msg: str) -> None:
|
||||
for char in itertools.cycle(r'\|/-'):
|
||||
status = f'\r{char} {msg}'
|
||||
print(status, flush=True, end='')
|
||||
try:
|
||||
await asyncio.sleep(.1)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
print('THIS WILL NEVER BE OUTPUT')
|
||||
|
||||
async def check(n: int) -> int:
|
||||
return primes.is_prime(n) # <4>
|
||||
|
||||
async def supervisor(n: int) -> int:
|
||||
spinner = asyncio.create_task(spin('thinking!')) # <1>
|
||||
print('spinner object:', spinner) # <2>
|
||||
result = await check(n) # <3>
|
||||
spinner.cancel() # <5>
|
||||
return result
|
||||
# end::SPINNER_ASYNC_EXPERIMENT[]
|
||||
|
||||
def main() -> None:
|
||||
n = 5_000_111_000_222_021
|
||||
result = asyncio.run(supervisor(n))
|
||||
msg = 'is' if result else 'is not'
|
||||
print(f'{n:,} {msg} prime')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
58
19-concurrency/primes/spinner_prime_async_nap.py
Normal file
58
19-concurrency/primes/spinner_prime_async_nap.py
Normal file
@@ -0,0 +1,58 @@
|
||||
# spinner_prime_async_nap.py
|
||||
|
||||
# credits: Example by Luciano Ramalho inspired by
|
||||
# Michele Simionato's multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||
|
||||
import asyncio
|
||||
import itertools
|
||||
import math
|
||||
|
||||
# tag::PRIME_NAP[]
|
||||
async def is_prime(n):
|
||||
if n < 2:
|
||||
return False
|
||||
if n == 2:
|
||||
return True
|
||||
if n % 2 == 0:
|
||||
return False
|
||||
|
||||
root = math.isqrt(n)
|
||||
for i in range(3, root + 1, 2):
|
||||
if n % i == 0:
|
||||
return False
|
||||
if i % 100_000 == 1: # <2>
|
||||
await asyncio.sleep(0)
|
||||
return True
|
||||
# end::PRIME_NAP[]
|
||||
|
||||
|
||||
async def spin(msg: str) -> None:
|
||||
for char in itertools.cycle(r'\|/-'):
|
||||
status = f'\r{char} {msg}'
|
||||
print(status, flush=True, end='')
|
||||
try:
|
||||
await asyncio.sleep(.1)
|
||||
except asyncio.CancelledError:
|
||||
break
|
||||
blanks = ' ' * len(status)
|
||||
print(f'\r{blanks}\r', end='')
|
||||
|
||||
async def check(n: int) -> int:
|
||||
return await is_prime(n) # <4>
|
||||
|
||||
async def supervisor(n: int) -> int:
|
||||
spinner = asyncio.create_task(spin('thinking!')) # <1>
|
||||
print('spinner object:', spinner) # <2>
|
||||
result = await check(n) # <3>
|
||||
spinner.cancel() # <5>
|
||||
return result
|
||||
|
||||
def main() -> None:
|
||||
n = 5_000_111_000_222_021
|
||||
result = asyncio.run(supervisor(n))
|
||||
msg = 'is' if result else 'is not'
|
||||
print(f'{n:,} {msg} prime')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
43
19-concurrency/primes/spinner_prime_proc.py
Normal file
43
19-concurrency/primes/spinner_prime_proc.py
Normal file
@@ -0,0 +1,43 @@
|
||||
# spinner_prime_proc.py
|
||||
|
||||
# credits: Adapted from Michele Simionato's
|
||||
# multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||
|
||||
import itertools
|
||||
from multiprocessing import Process, Event
|
||||
from multiprocessing import synchronize
|
||||
|
||||
from primes import is_prime
|
||||
|
||||
def spin(msg: str, done: synchronize.Event) -> None: # <1>
|
||||
for char in itertools.cycle(r'\|/-'): # <2>
|
||||
status = f'\r{char} {msg}' # <3>
|
||||
print(status, end='', flush=True)
|
||||
if done.wait(.1): # <4>
|
||||
break # <5>
|
||||
blanks = ' ' * len(status)
|
||||
print(f'\r{blanks}\r', end='') # <6>
|
||||
|
||||
def check(n: int) -> int:
|
||||
return is_prime(n)
|
||||
|
||||
def supervisor(n: int) -> int: # <1>
|
||||
done = Event() # <2>
|
||||
spinner = Process(target=spin,
|
||||
args=('thinking!', done)) # <3>
|
||||
print(f'spinner object: {spinner}') # <4>
|
||||
spinner.start() # <5>
|
||||
result = check(n) # <6>
|
||||
done.set() # <7>
|
||||
spinner.join() # <8>
|
||||
return result
|
||||
|
||||
def main() -> None:
|
||||
n = 5_000_111_000_222_021
|
||||
result = supervisor(n) # <9>
|
||||
msg = 'is' if result else 'is not'
|
||||
print(f'{n:,} {msg} prime')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
42
19-concurrency/primes/spinner_prime_thread.py
Normal file
42
19-concurrency/primes/spinner_prime_thread.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# spinner_prime_thread.py
|
||||
|
||||
# credits: Adapted from Michele Simionato's
|
||||
# multiprocessing example in the python-list:
|
||||
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
|
||||
|
||||
import itertools
|
||||
from threading import Thread, Event
|
||||
|
||||
from primes import is_prime
|
||||
|
||||
def spin(msg: str, done: Event) -> None: # <1>
|
||||
for char in itertools.cycle(r'\|/-'): # <2>
|
||||
status = f'\r{char} {msg}' # <3>
|
||||
print(status, end='', flush=True)
|
||||
if done.wait(.1): # <4>
|
||||
break # <5>
|
||||
blanks = ' ' * len(status)
|
||||
print(f'\r{blanks}\r', end='') # <6>
|
||||
|
||||
def check(n: int) -> int:
|
||||
return is_prime(n)
|
||||
|
||||
def supervisor(n: int) -> int: # <1>
|
||||
done = Event() # <2>
|
||||
spinner = Thread(target=spin,
|
||||
args=('thinking!', done)) # <3>
|
||||
print(f'spinner object: {spinner}') # <4>
|
||||
spinner.start() # <5>
|
||||
result = check(n) # <6>
|
||||
done.set() # <7>
|
||||
spinner.join() # <8>
|
||||
return result
|
||||
|
||||
def main() -> None:
|
||||
n = 5_000_111_000_222_021
|
||||
result = supervisor(n) # <9>
|
||||
msg = 'is' if result else 'is not'
|
||||
print(f'{n:,} {msg} prime')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
474
19-concurrency/primes/stats-procs.ipynb
Normal file
474
19-concurrency/primes/stats-procs.ipynb
Normal file
File diff suppressed because one or more lines are too long
65
19-concurrency/primes/threads.py
Normal file
65
19-concurrency/primes/threads.py
Normal file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
"""
|
||||
threads.py: shows that Python threads are slower than
|
||||
sequential code for CPU-intensive work.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from queue import SimpleQueue
|
||||
from time import perf_counter
|
||||
from typing import NamedTuple
|
||||
from threading import Thread
|
||||
|
||||
from primes import is_prime, NUMBERS
|
||||
|
||||
class PrimeResult(NamedTuple): # <3>
|
||||
n: int
|
||||
prime: bool
|
||||
elapsed: float
|
||||
|
||||
JobQueue = SimpleQueue[int]
|
||||
ResultQueue = SimpleQueue[PrimeResult]
|
||||
|
||||
def check(n: int) -> PrimeResult:
|
||||
t0 = perf_counter()
|
||||
res = is_prime(n)
|
||||
return PrimeResult(n, res, perf_counter() - t0)
|
||||
|
||||
def worker(jobs: JobQueue, results: ResultQueue) -> None:
|
||||
while n := jobs.get():
|
||||
results.put(check(n))
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2: # <1>
|
||||
workers = os.cpu_count() or 1 # make mypy happy
|
||||
else:
|
||||
workers = int(sys.argv[1])
|
||||
|
||||
print(f'Checking {len(NUMBERS)} numbers with {workers} threads:')
|
||||
|
||||
jobs: JobQueue = SimpleQueue() # <2>
|
||||
results: ResultQueue = SimpleQueue()
|
||||
t0 = perf_counter()
|
||||
|
||||
for n in NUMBERS: # <3>
|
||||
jobs.put(n)
|
||||
|
||||
for _ in range(workers):
|
||||
proc = Thread(target=worker, args=(jobs, results)) # <4>
|
||||
proc.start() # <5>
|
||||
jobs.put(0) # <6>
|
||||
|
||||
while True:
|
||||
n, prime, elapsed = results.get() # <7>
|
||||
label = 'P' if prime else ' '
|
||||
print(f'{n:16} {label} {elapsed:9.6f}s')
|
||||
if jobs.empty(): # <8>
|
||||
break
|
||||
|
||||
elapsed = perf_counter() - t0
|
||||
print(f'Total time: {elapsed:.2f}s')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user