update from O'Reilly repo
This commit is contained in:
51
20-concurrency/primes/py36/primes.py
Executable file
51
20-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
20-concurrency/primes/py36/procs.py
Executable file
71
20-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[]
|
||||
Reference in New Issue
Block a user