如何将 class 对象列表传递给 multiprocessing.process

How to pass a list of class object into a multiprocessing.process

我有一盒原子,大约200k,我想计算原子之间的距离。没有使用并行方法进行此计算确实花费了很长时间。所以我想使用 pool.map 来帮助我解决这个问题。我首先将盒子切成几个小单元,并定义了一个单元对象,其中包含该单元内的所有原子信息。但是,当我未能将单元格对象列表传递给进程时。我是这个多处理任务的初学者,有人知道如何解决这个问题吗?这是我的脚本的简化版:

class atoms():
    def __init__(self):
        self.__idx__ = 0 # Has other function to change this idx and coord
        self.__coord__ = [x, y, z] 
    def getIdx(self):
        return self.__idx__

class cell():
    def __init__(self):
        self.__idx__ = 0
        self.__atoms__ = [atom1, ...,]
    def outInfo(self):
        for a in self.__atoms__:
            print(a.getIdx())

from multiprocessing import Process, Value, Array

def f(cell_lists):
    for c in cell_lists:
        print(c.outInfo())

if __name__ == '__main__':
    cell_lists = [cell1, cell2, ..., cell8]
    p = Process(target=f, args=(cell_lists ))
    p.start()
    p.join()

错误消息是“PicklingError:无法腌制:它与 cell.Cell 不是同一个对象”

感谢迈克的建议。我弄清楚了问题所在。因为我使用 spyder 来编辑脚本。每次,为了节省时间,我都没有创建新的单元格项目,而是使用生成的单元格项目。这可能会将 python3 误导到另一个单元格对象。避免此问题的简单方法是重新启动脚本或创建新的单元格对象,以避免使用内存中生成的旧单元格对象。