我有一个代码,它应该检查给定数字范围内的素数。我希望它能并行执行。然而,我没能让它运转起来。根据我如何将数字传递给它,它要么工作并正确检查素数,但不并行运行,要么不检查素数,而是启动许多python进程,显然并行化什么都不做。两者显然都不是我所希望的。
我的函数is_prime()
应该检查一个数字是否是素数。在独立模式下,它正是这样做的。
import time
import multiprocessing
def is_prime(n):
if (n <= 1) :
return 'not a prime number'
if (n <= 3) :
return 'prime number'
if (n % 2 == 0 or n % 3 == 0) :
return 'not a prime number'
i = 5
while(i * i <= n):
if (n % i == 0 or n % (i + 2) == 0) :
return 'not a prime number'
i = i + 6
return 'prime number'
multiprocessing_func(x)
函数应该为它得到的每个数字打印,如果该数字是素数,则由is_prime()
检查。如果我定期调用函数,这也同样有效。
def multiprocessing_func(x):
time.sleep(2)
print('{} is {}'.format(x, is_prime(x)))
现在,如果我想与多处理模块并行运行上面的函数,我尝试像下面的代码中那样,用target=multiprocessing_func, args=(i,)
将参数传递给上面的函数。这导致启动许多python进程,但不会检查素数。如果我用target=multiprocessing_func(i), args=(i,)
运行它,它会检查素数,但不是并行的。
if __name__ == '__main__':
starttime = time.time()
processes = []
for i in range(1,200):
p = multiprocessing.Process(target=multiprocessing_func, args=(i,))
processes.append(p)
p.start()
for process in processes:
process.join()
print('Time taken = {} seconds'.format(time.time() - starttime))
有人知道我在这里做错了什么吗?我真的不明白。非常感谢你的帮助!
我在Python 2.7中运行它,它运行得很好。
代码:
import multiprocessing
import time
def is_prime(n):
if (n <= 1):
return 'not a prime number'
if (n <= 3):
return 'prime number'
if (n % 2 == 0 or n % 3 == 0):
return 'not a prime number'
i = 5
while (i * i <= n):
if (n % i == 0 or n % (i + 2) == 0):
return 'not a prime number'
i = i + 6
return 'prime number'
def multiprocessing_func(x):
time.sleep(2)
print('{} is {}'.format(x, is_prime(x)))
if __name__ == '__main__':
starttime = time.time()
processes = []
for i in range(1, 200):
p = multiprocessing.Process(target=multiprocessing_func, args=(i,))
processes.append(p)
p.start()
for process in processes:
process.join()
print('Time taken = {} seconds'.format(time.time() - starttime))
输出:
3 is prime number1 is not a prime number
2 is prime number
5 is prime number6 is not a prime number
7 is prime number8 is not a prime number
9 is not a prime number
10 is not a prime number
14 is not a prime number12 is not a prime number16 is not a prime number
15 is not a prime number
13 is prime number
4 is not a prime number
17 is prime number
26 is not a prime number
21 is not a prime number24 is not a prime number
29 is prime number
20 is not a prime number27 is not a prime number22 is not a prime number19 is prime number
25 is not a prime number
18 is not a prime number
30 is not a prime number11 is prime number
23 is prime number
28 is not a prime number33 is not a prime number31 is prime number
32 is not a prime number35 is not a prime number34 is not a prime number
39 is not a prime number36 is not a prime number38 is not a prime number37 is prime number
40 is not a prime number
41 is prime number
42 is not a prime number43 is prime number
45 is not a prime number44 is not a prime number
46 is not a prime number47 is prime number48 is not a prime number
49 is not a prime number
50 is not a prime number
52 is not a prime number51 is not a prime number
53 is prime number54 is not a prime number55 is not a prime number
56 is not a prime number
59 is prime number
57 is not a prime number
61 is prime number
62 is not a prime number60 is not a prime number
64 is not a prime number63 is not a prime number58 is not a prime number
67 is prime number65 is not a prime number66 is not a prime number68 is not a prime number
69 is not a prime number
70 is not a prime number72 is not a prime number
71 is prime number
74 is not a prime number73 is prime number
75 is not a prime number
78 is not a prime number
77 is not a prime number
80 is not a prime number
83 is prime number
85 is not a prime number79 is prime number
81 is not a prime number
76 is not a prime number84 is not a prime number
82 is not a prime number
87 is not a prime number89 is prime number
88 is not a prime number
90 is not a prime number92 is not a prime number91 is not a prime number
86 is not a prime number
95 is not a prime number
93 is not a prime number94 is not a prime number
96 is not a prime number
97 is prime number99 is not a prime number98 is not a prime number
101 is prime number
103 is prime number
100 is not a prime number
105 is not a prime number
102 is not a prime number107 is prime number
104 is not a prime number
106 is not a prime number
108 is not a prime number
110 is not a prime number
109 is prime number
112 is not a prime number111 is not a prime number
113 is prime number114 is not a prime number
115 is not a prime number
116 is not a prime number
117 is not a prime number
118 is not a prime number
120 is not a prime number119 is not a prime number
121 is not a prime number
122 is not a prime number123 is not a prime number
124 is not a prime number
125 is not a prime number
126 is not a prime number
127 is prime number
128 is not a prime number
130 is not a prime number129 is not a prime number
132 is not a prime number
131 is prime number
133 is not a prime number
134 is not a prime number
135 is not a prime number
136 is not a prime number
139 is prime number
138 is not a prime number
137 is prime number
140 is not a prime number142 is not a prime number
141 is not a prime number
143 is not a prime number
145 is not a prime number
144 is not a prime number
146 is not a prime number147 is not a prime number
148 is not a prime number
149 is prime number
150 is not a prime number
151 is prime number
154 is not a prime number
152 is not a prime number153 is not a prime number
155 is not a prime number
156 is not a prime number
158 is not a prime number157 is prime number
159 is not a prime number
161 is not a prime number160 is not a prime number
162 is not a prime number
163 is prime number
164 is not a prime number
165 is not a prime number
167 is prime number166 is not a prime number
168 is not a prime number
169 is not a prime number
170 is not a prime number
171 is not a prime number
172 is not a prime number173 is prime number
174 is not a prime number
175 is not a prime number
176 is not a prime number
178 is not a prime number
179 is prime number
177 is not a prime number
181 is prime number
180 is not a prime number
183 is not a prime number182 is not a prime number
184 is not a prime number
185 is not a prime number186 is not a prime number
187 is not a prime number
188 is not a prime number
189 is not a prime number
190 is not a prime number
192 is not a prime number
191 is prime number
193 is prime number
194 is not a prime number
195 is not a prime number
196 is not a prime number197 is prime number
198 is not a prime number199 is prime number
Time taken = 2.24506902695 seconds
Process finished with exit code 0
您的代码是正确的。
创建一个流程是一项繁重的任务,而且您的功能非常快,所以您可能会得到一个流程并快速运行,它会在准备和启动下一个流程之前退出。
注意:从is_prime()
的逻辑来看,整数越小,执行函数所需的时间就越短。因此,在这种情况下,查看流程也没有帮助。
我用修改了你的代码
time.sleep(random.randint(2, 6))
所以现在你在每个过程中都有更多的时间(而且是随机的(。
但是,您可能必须为每个进程计算更大的块,以便为进程提供一些工作(并将它们放入内核调度器((例如,检查multiprocessing_func
中的10_000素数,主程序只发送块的开始(,或者只检查更大的数字。
所以:您的代码是正确的,但CPU并没有真正的工作,以便真正完整地看到多处理。