Created
January 4, 2018 08:14
-
-
Save seozed/70d4397e0201f079a954a93525dcbbc7 to your computer and use it in GitHub Desktop.
利用concurrent.futures实现多进程示例
This file contains 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 concurrent.futures | |
import math | |
PRIMES = [ | |
112272535095293, | |
112582705942171, | |
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 main(): | |
with concurrent.futures.ProcessPoolExecutor() as executor: | |
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)): | |
print('%d is prime: %s' % (number, prime)) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment