Python 多处理 - 'TypeError('self.profile cannot be converted to a Python object for pickling',)'
Python multiprocessing - 'TypeError('self.profile cannot be converted to a Python object for pickling',)'
我在 Python 中定义了一个函数,它使用 Gambit 库来找到博弈论模型的纳什均衡。我有 20 个不同的游戏,并且已经能够编写一个 for 循环来找到每个游戏的纳什均衡。给定游戏的输出如下所示:
('Polynomial Systems Method: ', [<NashProfile for 'family': [[0.0,
0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0]]>])
现在我正在尝试并行化此任务,使用 multiprocessing
包中的 apply_async
,使用如下代码:
# Open the pool for parallelization
pool = mp.Pool(mp.cpu_count())
# Run a loop in parallel to find the Nash equilibria of all the families
nasheqobj = {f:pool.apply_async(find_nash, args=[f]) for f in range(20)}
pool.close()
pool.join()
# Retrieve the output of the loop
nasheq_fams = {}
for f in range(20):
nasheq_fams[f] = nasheqobj[f].get()
print('Results from running in parallel: ', nasheq_fams)
但是,当我尝试检索存储在 nasheqobj
中的循环输出以创建字典 nasheq_fams
时,我收到来自 nasheq_fams[f] = nasheqobj[f].get()
的错误:
raise self._value multiprocessing.pool.MaybeEncodingError: Error
sending result: '[<NashProfile for 'family': [[0.0, 1.0, 0.0, 0.0],
[0.0, 1.0]]>]'. Reason: 'TypeError('self.profile cannot be converted
to a Python object for pickling',)'
问题似乎与我的函数输出的格式化方式有关(例如,[<NashProfile for 'family': [[0.0, 1.0, 0.0, 0.0], [0.0, 1.0]]>]
),但我不知道它有什么问题...
谢谢大家的评论。我做了更多的研究,结果发现 NashProfile class 不是纯粹的 Python class 并且不支持腌制。最后,我将 NashProfile 中的信息保存到一个列表中,然后传递给它,它起作用了。
我在 Python 中定义了一个函数,它使用 Gambit 库来找到博弈论模型的纳什均衡。我有 20 个不同的游戏,并且已经能够编写一个 for 循环来找到每个游戏的纳什均衡。给定游戏的输出如下所示:
('Polynomial Systems Method: ', [<NashProfile for 'family': [[0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0]]>])
现在我正在尝试并行化此任务,使用 multiprocessing
包中的 apply_async
,使用如下代码:
# Open the pool for parallelization
pool = mp.Pool(mp.cpu_count())
# Run a loop in parallel to find the Nash equilibria of all the families
nasheqobj = {f:pool.apply_async(find_nash, args=[f]) for f in range(20)}
pool.close()
pool.join()
# Retrieve the output of the loop
nasheq_fams = {}
for f in range(20):
nasheq_fams[f] = nasheqobj[f].get()
print('Results from running in parallel: ', nasheq_fams)
但是,当我尝试检索存储在 nasheqobj
中的循环输出以创建字典 nasheq_fams
时,我收到来自 nasheq_fams[f] = nasheqobj[f].get()
的错误:
raise self._value multiprocessing.pool.MaybeEncodingError: Error sending result: '[<NashProfile for 'family': [[0.0, 1.0, 0.0, 0.0], [0.0, 1.0]]>]'. Reason: 'TypeError('self.profile cannot be converted to a Python object for pickling',)'
问题似乎与我的函数输出的格式化方式有关(例如,[<NashProfile for 'family': [[0.0, 1.0, 0.0, 0.0], [0.0, 1.0]]>]
),但我不知道它有什么问题...
谢谢大家的评论。我做了更多的研究,结果发现 NashProfile class 不是纯粹的 Python class 并且不支持腌制。最后,我将 NashProfile 中的信息保存到一个列表中,然后传递给它,它起作用了。