在多处理中使用 pool.map 时发生酸洗错误

Pickling error while using pool.map in multiprocessing

我有一个函数如下:

def func1(date1,date2):
 #calculations

if __name__=='__main__':
    pool = Pool(processes=4)
    dates = [[dt.datetime(2016,6,17),dt.datetime(2016,6,23)],[dt.datetime(2016,6,24),dt.datetime(2016,6,30)],[dt.datetime(2016,7,1),dt.datetime(2016,7,7)],[dt.datetime(2016,7,8),dt.datetime(2016,7,14)]]
    result=pool.map(lambda x: func1(x),dates)

我看到以下错误:

    File "Location_Factors_C.py", line 204, in <module>
    result=pool.map(lambda x: func1(x),dates)
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 251, in map
    return self.map_async(func, iterable, chunksize).get()
  File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
    raise self._value
cPickle.PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed

看起来你需要一个星号

result=pool.map(lambda x: func1(x),dates)

应该是:

result=pool.map(lambda x: func1(*x),dates)

您的 func1() 需要 2 个参数,而您只传入一个列表。

更改我的函数以接受日期列表:

def func1(datelist):
   date1 = datelist[0]
   date2 = datelist[1]


if __name__=='__main__':
    pool = Pool(processes=4)
    dates = [[dt.datetime(2016,6,17),dt.datetime(2016,6,23)],[dt.datetime(2016,6,24),dt.datetime(2016,6,30)],[dt.datetime(2016,7,1),dt.datetime(2016,7,7)],[dt.datetime(2016,7,8),dt.datetime(2016,7,14)]]
    result=pool.map(func1,dates)