类对象 Python.causes 中的 ProcessPoolExecutor:无法腌制“weakref”对象

问题描述

我有以下一段代码,它抛出“无法腌制‘weakref’对象”。

我见过很多例子,其中人们用 if __name__ == '__main__' 包装 ProcesspoolExecutor,但这在我的班级中是不可能的。没有关于如何在 python 类中使用 ProcesspoolExecutor 的示例。

作为参考,ThreadPoolExecutor 工作正常。

有人知道怎么做吗?

堆栈跟踪是:

  File "C:\Users\mikke\ALG_EXP\ThreeSum\pythonSol\Implementations\near_linear_alg\RandomizedMultiThreaded.py",line 35,in fasterSubsetSum
    S = self.sumSet(S,f.result(),t)
  File "C:\python38\lib\concurrent\futures\_base.py",line 439,in result
    return self.__get_result()
  File "C:\python38\lib\concurrent\futures\_base.py",line 388,in __get_result
    raise self._exception
  File "C:\python38\lib\multiprocessing\queues.py",line 239,in _Feed
    obj = _ForkingPickler.dumps(obj)
  File "C:\python38\lib\multiprocessing\reduction.py",line 51,in dumps
    cls(buf,protocol).dump(obj)
TypeError: cannot pickle 'weakref' object

代码是:

import concurrent.futures
import math
import threading

import numpy as np
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcesspoolExecutor,as_completed
from Implementations.helpers.Helper import toNumbers
from Implementations.near_linear_alg.RandomizedBase import NearLinearBase


class RandomizedMultiThreaded(NearLinearBase):

    def __init__(self,debug):
        super().__init__(debug,0)
        self.executor = ProcesspoolExecutor(max_workers=12)

    def fasterSubsetSum(self,Z,t,delta):
        n = len(Z)
        self.n = n
        Z = np.array(Z)
        Zi = self.partitionIntoLayers(Z,n,t)
        S = [1]
        if len(Zi[0]) > 1:
            S = Zi[0]
        futures = list()
        for i in range(1,len(Zi)):
            z = np.array(Zi[i])
            if len(z) > 1:
                if len(z) > 1:
                    ans = self.executor.submit(self.ColorCodingLayerMulti,z,pow(2,i + 1) - 1,delta / (math.ceil(math.log2(n))))
                    futures.append(ans)
        for f in futures:
            S = self.sumSet(S,t)
        return toNumbers(S)

解决方法

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

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

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