[英]python multiprocessing error about apply_async
这是我的代码段:
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多重处理来优化代码速度,所以我对多重处理一无所知,而且我无法理解此错误,有人可以帮我吗?
非常感谢
哦,知道了,原因是我的参数位于apply_async()的args中,可能是该args不支持对象参数。 让我详细介绍一下:在我的代码中, background = Image.open('background')
,如果我将其从pool.apply_async(func , args = (size , elements , elementSize , elementType , elementLine , elementGroup , imageElement , background))
,就可以了!
但是当我尝试这样做时:从PIL导入多处理import Image
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()
它实际上是可行的,那么这种情况是什么原因呢? 尽管解决了我的问题,我仍然不明白发生了什么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.