如何从多处理池作为数据帧返回多个值?

问题描述 投票:0回答:1

我为pqd设置了参数arima的顺序数组:

[(0, 0, 0),
 (0, 1, 0),
 (0, 2, 0),
 (0, 3, 0),
 (1, 0, 0),
 (1, 1, 0),
 (1, 2, 0),
 (1, 3, 0),
 (2, 0, 0),
 (2, 1, 0),
 (2, 2, 0),
 (2, 3, 0)]

我想通过多重处理使用该参数顺序评估有马的性能。但是我有两个问题:

  1. eval函数需要3个参数
  2. eval函数返回2个结果

如何将错误和模型的结果都作为数据框返回?

这是我尝试过的:

from multiprocessing import Pool
#this computes error and return a model
def eval_model_parameters(a,b,order):
    #use order PARAMETER somehow to compute model 
    error = a*b/2
    model = a
    return [error,model]

p = [0, 1 , 2]
d = [0, 1 , 2 ,3]
q = [0]
pdq = list(itertools.product(p, d, q)) 

p = Pool(7)
func = eval_model_parameters(1, 2, pdq)
res  = p.map(func, pdq)  
p.close()

我尝试这样做是为了将参数传递给函数

func = eval_model_parameters(1, 2, pdq)

然后返回结果

res  = p.map(func, pdq) 

但我知道

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "Anaconda3\lib\multiprocessing\pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "Anaconda3\lib\multiprocessing\pool.py", line 44, in mapstar
    return list(map(*args))
TypeError: 'list' object is not callable
"""

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-53-7743fd9ca60b> in <module>
     13 p = Pool(7)
     14 func = eval_model_parameters(1, 2, pdq)
---> 15 res  = p.map(func, pdq)
     16 p.close()
     17 

Anaconda3\lib\multiprocessing\pool.py in map(self, func, iterable, chunksize)
    266         in a list that is returned.
    267         '''
--> 268         return self._map_async(func, iterable, mapstar, chunksize).get()
    269 
    270     def starmap(self, func, iterable, chunksize=None):

Anaconda3\lib\multiprocessing\pool.py in get(self, timeout)
    655             return self._value
    656         else:
--> 657             raise self._value
    658 
    659     def _set(self, i, obj):

TypeError: 'list' object is not callable

完成此操作的正确方法是什么?

python multiprocessing pool
1个回答
0
投票

pool.map函数的第一个参数应为可调用对象。在您的情况下,您自己调用了该函数,然后将结果传递给pool.map函数。尝试将函数本身传递给pool.map,如下所示:

p.map(eval_model_parameters, pdq)

现在,当您更改上述行并运行代码时,您将看到pdq列表的元组作为单个参数传递。要解决此问题,请遵循此问题multiple args to pool.map

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.