问题:https://stackoverflow.com/questions/8640367/python-manager-dict-in-multiprocessing
使用 multiprocessing.Manager 来实现IPC的时候,遇到的问题
示例1
>>> from multiprocessing import Process, Manager >>> >>> manager = Manager() >>> d = manager.dict() >>> >>> def f(): ... d[‘k‘].append(3) ... print d ... >>> if __name__ == ‘__main__‘: ... d[‘k‘] = [] ... p = Process(target=f) ... p.start() ... p.join() ... {‘k‘: []} >>>
示例2
>>> from multiprocessing import Process, Manager >>> >>> def f(d): ... d[‘k‘].append(4) ... print type(d),d,id(d) ... >>> if __name__ == ‘__main__‘: ... manager = Manager() ... d = manager.dict({‘k‘:[]}) ... p = Process(target=f,args=(d,)) ... p.start() ... p.join() ... print type(d),d,id(d) ... <class ‘multiprocessing.managers.DictProxy‘> {‘k‘: []} 140607041173456 <class ‘multiprocessing.managers.DictProxy‘> {‘k‘: []} 140607041173456 >>>
为什么没有append成功呢?
再看示例3
>>> from multiprocessing import Process, Manager >>> >>> manager = Manager() >>> d = manager.dict() >>> >>> def f(): ... d[‘k‘] = 2 ... print d ... >>> if __name__ == ‘__main__‘: ... d[‘k‘] = [] ... p = Process(target=f) ... p.start() ... p.join() ... {‘k‘: 2} >>>
参考:http://www.cnblogs.com/havePassed/p/5168833.html
总结:
使用Manager方法时需要注意,在操作共享对象时候,除了赋值操作,其他的方法都作用在共享对象的拷贝上,并不会对共享对象生效。
原文地址:https://www.cnblogs.com/standby/p/8260647.html
时间: 2024-11-04 02:10:51