0
的運行功能創建的變量函數如果我有一個Python類我如何可以創建訪問在一個子類Python進程
from multiprocessing import Process
class A(Process):
def run(self):
self.var = "asdf"
def pprint(self):
print(self.var)
if __name__ == "__main__":
foo = A()
foo.start()
foo.pprint()
bar = A()
bar.pprint()
我得到的追蹤誤差
Traceback (most recent call last):
File "simple.py", line 13, in <module>
foo.pprint()
File "simple.py", line 8, in pprint
print(self.var)
AttributeError: 'A' object has no attribute 'var'
我可以在run函數中定義的訪問實例變量,以及在該範圍內定義的其他函數?
我會使用一個隊列從該外部進程中調用函數嗎? – user1876508
@ user1876508隊列主要用於傳輸數據。你可以創建一個自定義的'Manager'併爲跨進程的呼叫註冊一個類。請參閱http://docs.python.org/2/library/multiprocessing.html#customized-managers –