Python 多处理 - 执行时间增加,我做错了什么?

Python Multiprocessing - execution time increased, what am I doing wrong?

我正在做一个简单的多处理测试,但似乎有问题。我 运行 在 i5-6200U 2.3 Ghz 上使用 Turbo Boost。

from multiprocessing import Process, Queue
import time

def multiply(a,b,que): #add a argument to function for assigning a queue
    que.put(a*b) #we're putting return value into queue

if __name__ == '__main__':
    queue1 = Queue() #create a queue object
    jobs = []
    start_time = time.time()
#####PARALLEL####################################
    for i in range(0,400):
        p = p = Process(target= multiply, args= (5,i,queue1))
        jobs.append(p)
        p.start()

    for j in jobs:
        j.join()

    print("PARALLEL %s seconds ---" % (time.time() - start_time))
#####SERIAL################################
    start_time = time.time()
    for i in range(0,400):
        multiply(5,i,queue1)
    print("SERIAL %s seconds ---" % (time.time() - start_time))

输出:

PARALLEL 22.12951421737671 seconds ---
SERIAL 0.004009723663330078 seconds ---

非常感谢您的帮助。

这里有一个(愚蠢的)代码的简短示例,它获得了很好的加速。正如评论中已经提到的那样,它不会创建荒谬数量的进程,并且与进程间通信开销相比,每次远程函数调用完成的工作很高。

import multiprocessing as mp
import time

def factor(n):
    for i in range(n):
        pass
    return n

if __name__ == "__main__":
    ns = range(100000, 110000)

    s = time.time()
    p = mp.Pool(4)
    got = p.map(factor, ns)
    print(time.time() - s)
    assert got == list(ns)

    s = time.time()
    got = [factor(n) for n in ns]
    print(time.time() - s)
    assert got == list(ns)