我试图评估多处理池中的多个进程,但一直遇到错误,我无法解决原因......下面是代码的简化版本:< / p>
class Object_1():
def add_godd_spd_column()
def calculate_correlations(arg1, arg2, arg3):
return {'a': 1}
processes = {}
pool = Pool(processes=6)
for i in range(1, 10):
processes[i] = pool.apply_async(calculate_correlations,
args=(arg1, arg2, arg3,))
correlations = {}
for i in range(0, 10):
correlations[i] = processes[i].get()
这会返回以下错误:
Traceback (most recent call last):
File "./02_results.py", line 116, in <module>
correlations[0] = processes[0].get()
File "/opt/anaconda3/lib/python3.5/multiprocessing/pool.py", line 608, in get
raise self._value
File "/opt/anaconda3/lib/python3.5/multiprocessing/pool.py", line 385, in
_handle_tasks
put(task)
File "/opt/anaconda3/lib/python3.5/multiprocessing/connection.py", line 206, in send
self._send_bytes(ForkingPickler.dumps(obj))
File "/opt/anaconda3/lib/python3.5/multiprocessing/reduction.py", line 50, in dumps
cls(buf, protocol).dump(obj)
AttributeError: Can't pickle local object 'SCADA.add_good_spd_column.<locals>.calculate_correlations
当我拨打以下电话时: 相关性[0] .successful() 我收到以下错误:
Traceback (most recent call last):
File "./02_results.py", line 116, in <module>
print(processes[0].successful())
File "/opt/anaconda3/lib/python3.5/multiprocessing/pool.py", line 595, in
successful
assert self.ready()
AssertionError
这是因为在调用.get()之前,进程实际上还没有完成吗?正在评估的函数只返回一个绝对应该可以腌制的字典...
干杯,
答案 0 :(得分:2)
发生错误是因为pickling a function nested in another function is not supported,并且multiprocessing.Pool
需要将您作为参数传递的函数挑选到apply_async
以便在工作进程中执行它。您必须将函数移动到模块的顶层,或使其成为类的实例方法。请记住,如果将其设为实例方法,则类本身的实例也必须是可选择的。
是的,调用successful()
时出现断言错误是因为您在结果准备好之前调用它。 From the docs:
<强>
successful()
强>返回调用是否完成而不引发异常。如果结果尚未准备就会提升
AssertionError
。