gpt4 book ai didi

Python多处理池apply_async错误

转载 作者:行者123 更新时间:2023-11-28 17:09:05 24 4
gpt4 key购买 nike

我正在尝试评估多处理池中的多个进程,但一直遇到错误,我无法弄清楚为什么......下面是代码的简化版本:

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].成功()我收到以下错误:

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() 之前进程实际上并未完成吗?被评估的函数只返回一个字典,它绝对应该是 pickle-able...

干杯,

最佳答案

错误发生是因为 pickling a function nested in another function is not supported ,并且 multiprocessing.Pool 需要挑选您作为参数传递给 apply_async 的函数,以便在工作进程中执行它。您必须将函数移动到模块的顶层,或者使其成为类的实例方法。请记住,如果您将其设为实例方法,则类本身的实例也必须是可挑选的。

是的,调用 successful() 时发生断言错误是因为您在结果准备好之前调用它。 From the docs :

successful()

Return whether the call completed without raising an exception. Will raise AssertionError if the result is not ready.

关于Python多处理池apply_async错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48950516/

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