【发布时间】:2018-08-14 12:56:13
【问题描述】:
我在工作代码中遇到了这个问题,所以我无法展示它。但是我写了一些简短的例子,它准确地重现了错误并切断了冗余逻辑。
示例有两个文件:Example.py & ImportedExample.py。
Example.py
from multiprocessing import Process
from ImportedExample import Imported
class Example:
def __init__(self, number):
self.imported = Imported(number)
def func(example: Example):
print(example)
if __name__ == "__main__":
ex = Example(3)
p = Process(target=func, args=(ex,))
p.start()
ImportedExample.py
class Imported:
def __init__(self, number):
self.number = number
self.ref = self.__private_method
def __private_method(self):
print(self.number)
Traceback 看起来像这样:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File"C:\Python\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Python\Python36\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: 'Imported' object has no attribute '__private_method'
主要细节是当我将__private_method() 设为非私有(重命名为private_method())时,一切正常。
我不明白为什么会这样。有什么建议吗?
【问题讨论】:
-
酸洗似乎有些问题。
-
感谢@pacholik 提供详细解答。他的解释为我清除了一切,他的建议解决了我的问题
标签: python python-3.x multiprocessing pickle