forked from onnieBLT/Python-100-Days
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
609 additions
and
177 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
多进程和进程池的使用 | ||
多线程因为GIL的存在不能够发挥CPU的多核特性 | ||
对于计算密集型任务应该考虑使用多进程 | ||
time python3 example22.py | ||
real 0m11.512s | ||
user 0m39.319s | ||
sys 0m0.169s | ||
""" | ||
import concurrent.futures | ||
import math | ||
|
||
PRIMES = [ | ||
1116281, | ||
1297337, | ||
104395303, | ||
472882027, | ||
533000389, | ||
817504243, | ||
982451653, | ||
112272535095293, | ||
112582705942171, | ||
112272535095293, | ||
115280095190773, | ||
115797848077099, | ||
1099726899285419 | ||
] * 5 | ||
|
||
|
||
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() |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.