嵌套字典和慢函数的多处理共享值问题

时间:2019-02-17 01:18:04

标签: python-2.7 multiprocessing

在使用多处理功能调整嵌套字典中的值的同时,如果将嵌套字典复制到该值之后的过程很长,则不会被更新,并且看起来它正在覆盖另一个字典。

我已经研究了stackoverflow上的各种线程,但是找不到与此相关的线程。我知道我可以通过在调用long函数之后复制值来避免此问题,但是我想确保我正确使用了所有这些库和函数

from multiprocessing import Process,Manager

#works as expected
#===============================================================================
# def f(d):
#     shared = d["1"]
#     shared["1.1"] += 5
#     d["1"] = shared
#     print d
#==============================================================================

#doesn't work as expected
def f(d):
    shared = d["1"]
    shared["1.1"] += 5
    #used as an example of a slow function

    test = 0
    for i in range(10000000):
        test += 50
        test %= 3
        test *= i
    d["1"] = shared
    print d


if __name__ == '__main__':
    manager = Manager()
    d = manager.dict({"1":manager.dict({"1.1":0}),
                  "2":manager.dict({"1.1":0})})
    p = Process(target=f, args=(d,))
    p.start()
    p2 = Process(target=f, args=(d,))
    p2.start()   
    p.join()
    p2.join()

预期结果: {'1':{'1.1':5},'2':{'1.1':0}} {'1':{'1.1':10},'2':{'1.1':0}}

实际结果: {'1':{'1.1':5},'2':{'1.1':0}} {'1':{'1.1':5},'2':{'1.1':0}}

0 个答案:

没有答案