Created
April 20, 2020 20:14
-
-
Save nobbynobbs/7d4e792ad2787b8742ccf7a5f9a22f5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import signal | |
from concurrent.futures import ProcessPoolExecutor | |
from contextlib import suppress | |
from time import sleep | |
values = [40000000, 30000000, 20000000, 10000000] | |
PRIMES = [ | |
112272535095293, | |
112582705942171, | |
112272535095293, | |
115280095190773, | |
115797848077099, | |
112272535095293, | |
115280095190773, | |
115797848077099, | |
112272535095293, | |
115280095190773, | |
115797848077099, | |
112272535095293, | |
115280095190773, | |
115797848077099, | |
1099726899285419] | |
def is_prime(n): | |
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 register_signal_handler(): | |
signal.signal(signal.SIGINT, lambda _, __: None) | |
def main(): | |
with suppress(KeyboardInterrupt): | |
with ProcessPoolExecutor( | |
max_workers=4, | |
initializer=register_signal_handler | |
) as executor: | |
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): | |
print('%d is prime: %s' % (number, prime)) | |
while True: | |
print("i'am alive") | |
sleep(1) | |
print("Good bye") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment