简体   繁体   English

为什么此代码中的python多处理速度较慢?

[英]Why python multiprocessing in this code is slower?

I have this sample code. 我有此示例代码。 I'm making some math computation (graph theory) and I want to increase the speed so I decided to go with multiprocessing but surprisingly the same code runs even slower than the single process version. 我正在做一些数学计算(图形理论),并且想提高速度,所以我决定采用多处理,但是令人惊讶的是,同一代码的运行速度甚至比单进程版本还要慢。

I expect that if you have a list and you split it in half and start two processes it should take approximately half time. 我希望如果您有一个列表,并且将其分成两半并启动两个过程,则大约需要花费一半的时间。 I don't think I have some synchronization issue so why the hell is this so slow? 我不认为我有一些同步问题,所以为什么这么慢?

from multiprocessing import Process, Queue

def do_work(queue, aList):

    for elem in aList:
        newList.append(a_math_function(elem))

    queue.put(max(newList))
    return max(newList)

def foo():

    #I have let's say aList with 100000 objects

    q1 = Queue(); q2 = Queue()

    p1 = Process(target=do_work, args=[q1, aList[0: size/2])])
    p2 = Process(target=do_work, args=[q2, aList[size/2+1: size])])

    p1.start(); p2.start()
    p1.join(); p2.join()

    print(max(q1.get(),q2.get())

Instead of using multiprocessing, try cutting down on the amount of allocation you're doing. 不要尝试使用多处理,而要减少正在执行的分配量。 You're just filling RAM up with temporary lists (for example the halves of aList you pass the processes, and then the newList lists you create in each process). 您只是用临时列表填充RAM(例如,通过流程的aList的一半,然后在每个流程中创建的newList列表)。 Quite possibly this is the cause of the slowness as lots of allocation means lots of garbage collection. 这很可能是造成速度缓慢的原因,因为大量的分配意味着大量的垃圾回收。

Try replacing all your code with this and measuring the speed. 尝试以此替换所有代码并测量速度。

def foo():
    print max(a_math_function(x) for x in aList)

Use a multiprocessing.Pool.map() to distribute the workload between workers and get the result aggregated from it. 使用multiprocessing.Pool.map()在工作人员之间分配工作负载并从中获取结果汇总。

See the example here below http://docs.python.org/library/multiprocessing.html#multiprocessing.pool.AsyncResult.successful 请参见下面的示例http://docs.python.org/library/multiprocessing.html#multiprocessing.pool.AsyncResult.successful

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM