以下是来自http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes的调整示例
from multiprocessing import Process, Manager
def f(d):
print d # 1
print type(d)
if __name__ == '__main__':
manager = Manager()
d = manager.dict()
p = Process(target=f, args=(d))
p.start()
p.join()
我正在尝试做类似的事情
from multiprocessing import Process, Manager
class abcd(Process):
def __init__(self,d):
Process.__init__(self)
self.d = d
print self.d # 2
print type(self.d)
def run(self):
print self.d # 3
print type(self.d)
if __name__ == '__main__':
manager = Manager()
d = manager.dict()
proc = abcd(d)
proc.start()
真正困扰我的是,在标记为 1 和 2 的行中,我得到了我所期望的 {} - 空白字典。但在第 3 行打印
<DictProxy object, typeid 'dict' at 0x18ac9d0; '__str__()' failed>
从 Process 继承时我错过了什么吗?