猿问

多处理不运行函数

我有一个代码,应该检查给定数字范围内的素数。我希望它并行执行。但是,我无法让它运行。根据我将数字传递给它的方式,它要么工作并正确检查素数,但没有并行运行,要么它不检查素数,但启动许多 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))

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


红颜莎娜
浏览 88回答 2
2回答

哈士奇WWW

我在 Python 2.7 中运行它并且运行良好。代码:import multiprocessingimport timedef is_prime(n):&nbsp; &nbsp; if (n <= 1):&nbsp; &nbsp; &nbsp; &nbsp; return 'not a prime number'&nbsp; &nbsp; if (n <= 3):&nbsp; &nbsp; &nbsp; &nbsp; return 'prime number'&nbsp; &nbsp; if (n % 2 == 0 or n % 3 == 0):&nbsp; &nbsp; &nbsp; &nbsp; return 'not a prime number'&nbsp; &nbsp; i = 5&nbsp; &nbsp; while (i * i <= n):&nbsp; &nbsp; &nbsp; &nbsp; if (n % i == 0 or n % (i + 2) == 0):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 'not a prime number'&nbsp; &nbsp; &nbsp; &nbsp; i = i + 6&nbsp; &nbsp; return 'prime number'def multiprocessing_func(x):&nbsp; &nbsp; time.sleep(2)&nbsp; &nbsp; print('{} is {}'.format(x, is_prime(x)))if __name__ == '__main__':&nbsp; &nbsp; starttime = time.time()&nbsp; &nbsp; processes = []&nbsp; &nbsp; for i in range(1, 200):&nbsp; &nbsp; &nbsp; &nbsp; p = multiprocessing.Process(target=multiprocessing_func, args=(i,))&nbsp; &nbsp; &nbsp; &nbsp; processes.append(p)&nbsp; &nbsp; &nbsp; &nbsp; p.start()&nbsp; &nbsp; for process in processes:&nbsp; &nbsp; &nbsp; &nbsp; process.join()&nbsp; &nbsp; print('Time taken = {} seconds'.format(time.time() - starttime))输出:3 is prime number1 is not a prime number2 is prime number5 is prime number6 is not a prime number7 is prime number8 is not a prime number9 is not a prime number10 is not a prime number14 is not a prime number12 is not a prime number16 is not a prime number15 is not a prime number13 is prime number4 is not a prime number17 is prime number26 is not a prime number21 is not a prime number24 is not a prime number29 is prime number20 is not a prime number27 is not a prime number22 is not a prime number19 is prime number25 is not a prime number18 is not a prime number30 is not a prime number11 is prime number23 is prime number28 is not a prime number33 is not a prime number31 is prime number32 is not a prime number35 is not a prime number34 is not a prime number39 is not a prime number36 is not a prime number38 is not a prime number37 is prime number40 is not a prime number41 is prime number42 is not a prime number43 is prime number45 is not a prime number44 is not a prime number46 is not a prime number47 is prime number48 is not a prime number49 is not a prime number50 is not a prime number52 is not a prime number51 is not a prime number53 is prime number54 is not a prime number55 is not a prime number56 is not a prime number59 is prime number57 is not a prime number61 is prime number62 is not a prime number60 is not a prime number64 is not a prime number63 is not a prime number58 is not a prime number67 is prime number65 is not a prime number66 is not a prime number68 is not a prime number69 is not a prime number70 is not a prime number72 is not a prime number71 is prime number74 is not a prime number73 is prime number75 is not a prime number78 is not a prime number77 is not a prime number80 is not a prime number83 is prime number85 is not a prime number79 is prime number81 is not a prime number76 is not a prime number84 is not a prime number82 is not a prime number87 is not a prime number89 is prime number88 is not a prime number90 is not a prime number92 is not a prime number91 is not a prime number86 is not a prime number95 is not a prime number93 is not a prime number94 is not a prime number96 is not a prime number97 is prime number99 is not a prime number98 is not a prime number101 is prime number103 is prime number100 is not a prime number105 is not a prime number102 is not a prime number107 is prime number104 is not a prime number106 is not a prime number108 is not a prime number110 is not a prime number109 is prime number112 is not a prime number111 is not a prime number113 is prime number114 is not a prime number115 is not a prime number116 is not a prime number117 is not a prime number118 is not a prime number120 is not a prime number119 is not a prime number121 is not a prime number122 is not a prime number123 is not a prime number124 is not a prime number125 is not a prime number126 is not a prime number127 is prime number128 is not a prime number130 is not a prime number129 is not a prime number132 is not a prime number131 is prime number133 is not a prime number134 is not a prime number135 is not a prime number136 is not a prime number139 is prime number138 is not a prime number137 is prime number140 is not a prime number142 is not a prime number141 is not a prime number143 is not a prime number145 is not a prime number144 is not a prime number146 is not a prime number147 is not a prime number148 is not a prime number149 is prime number150 is not a prime number151 is prime number154 is not a prime number152 is not a prime number153 is not a prime number155 is not a prime number156 is not a prime number158 is not a prime number157 is prime number159 is not a prime number161 is not a prime number160 is not a prime number162 is not a prime number163 is prime number164 is not a prime number165 is not a prime number167 is prime number166 is not a prime number168 is not a prime number169 is not a prime number170 is not a prime number171 is not a prime number172 is not a prime number173 is prime number174 is not a prime number175 is not a prime number176 is not a prime number178 is not a prime number179 is prime number177 is not a prime number181 is prime number180 is not a prime number183 is not a prime number182 is not a prime number184 is not a prime number185 is not a prime number186 is not a prime number187 is not a prime number188 is not a prime number189 is not a prime number190 is not a prime number192 is not a prime number191 is prime number193 is prime number194 is not a prime number195 is not a prime number196 is not a prime number197 is prime number198 is not a prime number199 is prime numberTime taken = 2.24506902695 secondsProcess finished with exit code 0

qq_花开花谢_0

你的代码是正确的。创建一个进程是一项繁重的任务,你的功能非常快,所以你可能会得到一个进程并快速运行,它退出,然后再准备和启动下一个进程。注意:从你的逻辑来看is_prime(),整数越小,函数执行时间越短。因此,在这种情况下,查看流程也无济于事。我修改了你的代码:time.sleep(random.randint(2,&nbsp;6))所以现在你在每个过程中都有更多的时间(和随机的)。但是您可能必须为每个进程计算更大的块,以便为进程提供一些工作(并将它们放入内核调度程序)(例如,检查 10_000 个素数,主程序只发送块的开始),或者检查更大的multiprocessing_func数字.所以:你的代码是正确的,但 CPU 没有真正的工作,以便真正完整地看到多处理。
随时随地看视频慕课网APP

相关分类

Python
我要回答