类方法的并行执行引发 PicklingError

问题描述

我实现了一个类,它计算不同电池类型的可能配置..

因此,所有电池数据都存储在嵌套字典中,这是类本身的属性。每个子词典代表一种电池化学。

到目前为止,使用 NumPy 顺序运行代码,产生正确的结果。

因为应用过滤器很费时间,因此我想通过将每个子字典发送到过滤器进程来加快计算速度。

以下代码显示了我的目标的一个简化的最小示例:

import concurrent.futures

class Foo():
    __myData = {
            "1":    {"to":  1},"2":    {"to":  2},"3":    {"to":  3}}

    def processData(self,filterValue_:float):
        with concurrent.futures.ProcessPoolExecutor() as executor:
            results = [executor.submit(Foo.__applyFilter,subdict,filterValue_)\
                        for subdict in self.__myData.values()]
            
            for result in concurrent.futures.as_completed(results):
                print(result)
            # end for-loop -- print results
        # end with -- execute in parallel
    # end public method -- process data

    @staticmethod
    def __applyFilter(subdict_,value_):
        # do something
        subdict_['to'] += value_

        return subdict_
    # end private static method -- __applyFilter()
# end class -- Foo

if __name__=="__main__":
    myFoo = Foo()
    myFoo.processData(2)

此代码引发 PicklingError。我认为,发生这种情况是因为该方法专用于一个类实例,无法进行腌制。

问题
你能帮我在类方法上应用任何并行/多处理吗? 当多处理仅使用标准库时,我会很高兴。

非常感谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)