gpt4 book ai didi

Python 多处理 : cPickle. PicklingError:无法腌制

转载 作者:太空宇宙 更新时间:2023-11-04 05:36:43 27 4
gpt4 key购买 nike

<分区>

我正在尝试使用 python multiprocessing 模块来处理大量 url,我正在使用 multiprocessing.Pool 对象为其创建工作进程,如下所示。

from multiprocessing import Pool, TimeoutError, cpu_count

class MyClass:
def square(self, x):
return x*x

@staticmethod
def getNumbers():
return range(10)

def calculate(self):
pool = Pool(processes=min(cpu_count(),8))
results = [pool.apply(self.square,(i,)) for i in self.getNumbers()]
pool.close()
pool.join()
for result in results:
print result


if __name__ == '__main__':
instance = MyClass()
instance.calculate()

然而,上面的代码片段导致如下酸洗错误:

Traceback (most recent call last):
File "multi.py", line 24, in <module>
instance.calculate()
File "multi.py", line 15, in calculate
results = [pool.apply(self.square,(i,)) for i in self.getNumbers()]
File "/usr/lib/python2.7/multiprocessing/pool.py", line 244, in apply
return self.apply_async(func, args, kwds).get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 558, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

注意之前有人在 SO 上提出过类似的问题,但仍未得到答复:cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

编辑 给出了更好的示例代码示例

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com