Skip to content

Commit 07856d4

Browse files
committed
Python 3.7 example without some types
1 parent c7328ff commit 07856d4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from time import perf_counter
2+
from typing import Tuple, List, NamedTuple
3+
from threading import Thread
4+
from queue import SimpleQueue
5+
6+
from primes import is_prime, NUMBERS
7+
8+
class Result(NamedTuple): # <3>
9+
flag: bool
10+
elapsed: float
11+
12+
def check(n: int) -> Result: # <5>
13+
t0 = perf_counter()
14+
res = is_prime(n)
15+
return Result(res, perf_counter() - t0)
16+
17+
def job(n: int, results: SimpleQueue) -> None: # <6>
18+
results.put((n, check(n))) # <7>
19+
20+
def main() -> None:
21+
t0 = perf_counter()
22+
results = SimpleQueue() # type: ignore
23+
workers: List[Thread] = [] # <2>
24+
25+
for n in NUMBERS:
26+
worker = Thread(target=job, args=(n, results)) # <3>
27+
worker.start() # <4>
28+
workers.append(worker) # <5>
29+
30+
for _ in workers: # <6>
31+
n, (prime, elapsed) = results.get() # <7>
32+
label = 'P' if prime else ' '
33+
print(f'{n:16} {label} {elapsed:9.6f}s')
34+
35+
time = perf_counter() - t0
36+
print('Total time:', f'{time:0.2f}s')
37+
38+
if __name__ == '__main__':
39+
main()

0 commit comments

Comments
 (0)