为了账号安全,请及时绑定邮箱和手机立即绑定

多处理不运行函数

多处理不运行函数

红颜莎娜 2023-03-22 17:03:43
我有一个代码,应该检查给定数字范围内的素数。我希望它并行执行。但是,我无法让它运行。根据我将数字传递给它的方式,它要么工作并正确检查素数,但没有并行运行,要么它不检查素数,但启动许多 python 进程并且显然并行化什么都不做。两者显然都不是我所希望的。我的函数is_prime()应该检查一个数字是否是素数。在独立模式下,它正是这样做的。import timeimport multiprocessingdef 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 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

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



查看完整回答
反对 回复 2023-03-22
?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

你的代码是正确的。

创建一个进程是一项繁重的任务,你的功能非常快,所以你可能会得到一个进程并快速运行,它退出,然后再准备和启动下一个进程。

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

我修改了你的代码:

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

所以现在你在每个过程中都有更多的时间(和随机的)。

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

所以:你的代码是正确的,但 CPU 没有真正的工作,以便真正完整地看到多处理。


查看完整回答
反对 回复 2023-03-22
  • 2 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信