Python的map_async如何保持结果顺序?

时间:2013-10-30 18:02:44

标签: python-3.x multiprocessing

我正在尝试探索Python的py3.3多处理库,我注意到map_async函数中的奇怪结果,我无法解释。我一直期待从回调中存储的结果“乱序”。也就是说,如果我将多个任务提供给工作进程,则有些任务应该先于其他任务完成,而不一定按照它们被输入或存在于输入列表中的相同顺序。但是,我得到了一组有序的结果,与输入的任务完全一致。即使故意试图通过减慢某些进程来“破坏”某些进程(这可能会让其他进程在其之前完成),就会出现这种情况。

我在calculate函数中有一个打印声明,显示它们是按顺序计算的,但结果仍然是有序的。虽然我不确定我是否可以相信打印声明是一个很好的指标,表明事情实际上是计算乱序。

测试过程(一般示例): 构建一个对象列表,每个对象都包含一个整数。 将该对象列表作为参数提交给map_async,以及更新对象的numValue属性的函数“calculate”,其值为平方值。 然后“calculate”函数返回具有更新值的对象。

一些代码:

import time
import multiprocessing
import random

class NumberHolder():
    def __init__(self,numValue):
        self.numValue = numValue    #Only one attribute

def calculate(obj):
    if random.random() >= 0.5:
        startTime = time.time()
        timeWaster = [random.random() for x in range(5000000)] #Waste time.
        endTime = time.time()           #Establish end time
        print("%d object got stuck in here for %f seconds"%(obj.numValue,endTime-startTime))

#Main Process
if __name__ == '__main__':
    numbersToSquare = [x for x in range(0,100)]     #I'm 
    taskList = []

    for eachNumber in numbersToSquare:
        taskList.append(NumberHolder(eachNumber))   #Create a list of objects whose numValue is equal to the numbers we want to square

    results = [] #Where the results will be stored
    pool = multiprocessing.Pool(processes=(multiprocessing.cpu_count() - 1)) #Don't use all my processing power.
    r = pool.map_async(calculate, taskList, callback=results.append)  #Using fxn "calculate", feed taskList, and values stored in "results" list
    r.wait()                # Wait on the results from the map_async

results = results[0]    #All of the entries only exist in the first offset
for eachObject in results:      #Loop through them and show them
    print(eachObject.numValue)          #If they calc'd "out of order", I'd expect append out of order

我发现这个写得很好的回复,似乎支持map_async的结果“乱序”的想法:multiprocessing.Pool: When to use apply, apply_async or map?。我也在这里查看了文档(http://docs.python.org/3.3/library/multiprocessing.html)。对于map_async,它说明了这个方法“...如果指定了回调,那么它应该是一个可接受单个参数的可调用。当结果准备就绪时,将对其应用回调(除非调用失败)。回调应立即完成,否则处理结果的线程将被阻止“

我是否误解了这应该如何运作?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:11)

这是预期的行为。文档说:

  

返回结果对象的map()方法的变体。

“结果对象”只是一个容纳计算结果的容器类。当您致电r.wait()时,您需要等到所有结果汇总并按顺序排列。即使它不按顺序处理任务,结果仍然是原始顺序。

如果您希望在计算结果时产生结果,请使用imap_unordered