forked from DaSKITA/tiltify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
concurrency.py
49 lines (40 loc) · 1 KB
/
concurrency.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import concurrent.futures
import math
import random
import time
PRIMES = [
112272535095293,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
1099726899285419]
def is_prime(n):
chosen = random.choice([1, 5])
print(f"Wait {chosen} seconds")
time.sleep(chosen)
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True
def main():
futures = []
with concurrent.futures.ProcessPoolExecutor() as executor:
for number in PRIMES:
prime_future = executor.submit(is_prime, number)
futures.append(prime_future)
print("Whay!")
for fut in futures:
#if fut.done():
print(f"Outcome is: {fut.result()}")
# else:
# futures.append(fut)
if __name__ == '__main__':
main()