关于apply_async的python多处理错误

时间:2016-12-05 10:51:22

标签: python multiprocessing

这是我的代码段:

def getEnergyTerm(size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , Dis , background):
    pdb.set_trace()
    functionList = [alignCalc , whiteSpace , getBalanceGravityCenter , spread , dist , margin , textSize , textVar , minTextSize , textContrast , textOverlap , graphicTextOverlap , graphicBoundary ,  groupSizeVar , groupDistMean]
    pool = multiprocessing.Pool()

    result = []
    #whiteSpace(size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background)
    #pool.apply_async(whiteSpace , args = (size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background))
    for func in functionList:
        result.append(pool.apply_async(func , args = (size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background)))

    pool.close()
    pool.join()
    print result
    energy = {k:v for item in result for k , v in item.get().items()}
    return energy

functionList中的函数如下:

def whiteSpace(size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background):
    #pdb.set_trace()
    sum = size[0] * size[1]
    for item in range(len(elements)):
        sum -= elementSize[item][0] * elementSize[item][1]

    sum = sum * 1.0 / (size[0] * size[1])

    E_white_space =  -1.0 * sigmod(sum , alpha)
    res = {}
    res['whiteSpace'] = E_white_space
    return res

我使用pdb调试我的代码并发生错误,这是错误信息:

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 102, in worker
    task = get()
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 376, in get
    return recv()
TypeError: __new__() takes exactly 4 arguments (2 given)

我只是想使用python多处理来优化我的代码的速度,所以我不了解多处理,我无法理解这个错误,有人可以帮助我吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

哦,我明白了,原因是我在apply_async()&#s; args中的参数,可能是这个args不支持对象参数。让我详细说明一下: 在我的代码background = Image.open('background')中,如果我将其从pool.apply_async(func , args = (size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background))中删除,那就可以了!

但是当我尝试这个时:     导入多处理     来自PIL导入图像

def testFunc(y , x , calcY, list , str , img):
    a = [i*i for i in x]
    #b = [i+i for i in y]
    b = y[0]
    c = [i+i for i in list]
    res = {}
    res['a'] = a
    res['b'] = b
    res['c'] = c
    res['str'] = str
    img.paste(0 , (100 , 100 , 100 , 100))
    res['img'] = img

    return res



if __name__ == '__main__':
    p = multiprocessing.Pool()
    img = Image.new('RGBA' , (800 , 600))
    res = p.apply_async(testFunc , args = ((2 , 3) , [2 , 3 , 4] , False , [6 , 7 , 8 , 9] , 'hello world' , img))
    print res.get()

它确实有效,那么这种情况的原因是什么?尽管解决了我的问题,我仍然无法理解发生的事情