微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决类对象 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 举报,一经查实,本站将立刻删除。