我想从对象实例中调用一个方法为子程序。我在Windows 10上使用Python 3.6。
让我们创建一个简单的类:
class A:
def __init__(self):
self.a = "A"
def run(self):
print("Hello World")
测试
>>> a = A()
>>> a.run()
Hello World
然后通过MP
调用运行方法from multiprocessing import Process
if __name__ == "__main__":
p = Process(target=a.run)
p.start()
然后我有以下错误:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:UsersSynerlinkAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:UsersSynerlinkAppDataLocalProgramsPythonPython36libmultiprocessingspawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'A' on <module '__main__' (built-in)>
有人可以向我解释一下,如果有办法这样做?
不确定您的完整代码是什么样的,但这只是有效的。
from multiprocessing import Process
class A:
def __init__(self):
self.a = 'A'
def run(self):
print('Hello World')
if __name__ == '__main__':
a = A()
p = Process(target=a.run)
p.start()