多重处理无法运行功能

时间:2020-07-22 08:09:26

标签: python parallel-processing multiprocessing

我有一个代码,应该检查给定数字范围内的质数。我希望它可以并行执行。但是,我无法使其运行。根据我将数字传递给它的方式,它可以正常工作并检查素数,但不能并行运行,或者它不检查素数,但是会启动许多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))

有人知道吗,我在这里做错了什么?我真的不明白。非常感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我正在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

答案 1 :(得分:1)

您的验证码是正确的。

创建流程是一项繁重的任务,并且您的功能非常快,因此在准备和启动下一个流程之前,您可能会获得一个流程并快速运行,然后退出。

注意:从is_prime()的逻辑来看,整数越小,执行该功能所需的时间越短。因此,在这种情况下,查看进程也无济于事。

我用以下方法修改了您的代码

time.sleep(random.randint(2, 6))

所以现在您在每个过程中都有更多的时间(并且是随机的)。

但是可能您必须为每个进程计算更大的块,以便为进程做一些工作(并将它们放入内核调度程序中)(例如,检查multiprocessing_func中的10_000素数,而主程序只是发送块的开始),或检查更大的数字。

所以:您的代码是正确的,但是要真正完整地了解多处理,CPU尚无实际工作。