[英]Multiprocessing in Python 3.8
I'm trying to use multiprocessing in python for the first time.我第一次尝试在 python 中使用多处理。 I wrote a basic prime searching program and I want to run simultaneously it on each core.我写了一个基本的素数搜索程序,我想在每个核心上同时运行它。 The problem is: when the program does the multiprocessing, it not only does the 'primesearch' function but also the beginning of the code.问题是:当程序进行多处理时,它不仅会执行“primesearch”function,而且还会执行代码的开头。 My expected output would be a list of prime numbers between 0 and a limit, but it writes 16 times (I have 16 cores and 16 processes) "Enter a limit: "我预期的 output 将是一个介于 0 和一个限制之间的素数列表,但它写入 16 次(我有 16 个内核和 16 个进程)“输入限制:”
Here is my code:这是我的代码:
import time
import os
from multiprocessing import Process
# Defining lists
primes = []
processes = []
l = [0]
limit = int(input('Enter a limit: '))
def primesearch(lower,upper):
global primes
for num in range(lower, upper):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
primes.append(num)
# Start the clock
starter = time.perf_counter()
#Dividing data
step = limit // os.cpu_count()
for x in range(os.cpu_count()):
l.append(step * (x+1))
l[-1] = limit
#Multiprocessing
for init in range(os.cpu_count()):
processes.append(Process(target=primesearch, args=[l[init], l[init + 1],] ))
for process in processes:
process.start()
for process in processes:
process.join()
#End clock
finish = time.perf_counter()
print(primes)
print(f'Finished in {round(finish-starter, 2)} second')
What could be the problem?可能是什么问题呢?
You are using Windows - If you read the Python documenation for multiprocessing, it will reveal to you that you should protect your main code using if __name__==“__main__”:
This is because on Windows each process re-executes the complete main.py file. You are using Windows - If you read the Python documenation for multiprocessing, it will reveal to you that you should protect your main code using if __name__==“__main__”:
This is because on Windows each process re-executes the complete main.py文件。
This is used in pretty much every example in the documentation., and explained in the section at the end 'Programming guidelines'.这几乎在文档中的每个示例中都有使用,并在最后的“编程指南”部分中进行了解释。
See https://docs.python.org/3/library/multiprocessing.html见https://docs.python.org/3/library/multiprocessing.html
Except for the __main__
issue, your way of using primes as a global list doesn't seem to work.除了__main__
问题,您使用素数作为全局列表的方式似乎不起作用。 I imported Queue from multiprocessing and used primes = Queue()
and我从多处理导入队列并使用primes = Queue()
和
size = primes.qsize()
print([primes.get() for _ in range(size)])
primes.close()
in the main function and primes.put(num)
in your function.在 function 中的主要 function 和primes.put(num)
中。 I don't know if it's the best way, for me this works but if N > 12000 then the console freezes.我不知道这是否是最好的方法,对我来说这是可行的,但如果 N > 12000 则控制台会冻结。 Also, in this case, using multiprocessing is actually slightly slower than single process.此外,在这种情况下,使用多进程实际上比单进程稍慢。
If you aim for speed, you can test only to the square root of num, which saves about half of the time.如果你的目标是速度,你可以只测试 num 的平方根,这样可以节省大约一半的时间。 There are many optimizations you can do.您可以进行许多优化。 If you are testing huge numbers, you can use the Rabin-Miller algorithm.如果要测试大量数字,可以使用 Rabin-Miller 算法。 http://inventwithpython.com/cracking/chapter22.html http://inventwithpython.com/cracking/chapter22.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.