简体   繁体   English

Python多处理嵌套字典

[英]python multiprocessing nested dictionay

I try to use the multiprocessing tool in python (3.4.2) to speed up my performance. 我尝试使用python(3.4.2)中的多处理工具来提高性能。 My normal programm is running ok, but I have to process a large and nested dictionary in it. 我的常规程序运行正常,但是我必须在其中处理大型嵌套字典。 Attached a simplified version, which shows my problem. 随附了简化版,显示了我的问题。 If I use the TestProc directly it is working, with multiprocessing not. 如果我直接使用TestProc,则可以正常工作,而不能进行多重处理。

Thanks for your help! 谢谢你的帮助!

import multiprocessing

def TestProc(liste, results):

    for i in liste:

        results[i] = {'power':{'square': float(i)**2, 'cubic': 
        float(i)**3},'root':{'square': float(i)**(1/2), 'cubic': float(i)**(1/3)}}

if __name__ == "__main__":

    multiprocessing.freeze_support()
    results = multiprocessing.Manager().dict()

    results = {}
    liste = ['1','2','3','4','5']

    for i in liste:

        results[i] = multiprocessing.Manager().dict()

    print(results)

    #TestProc(liste, results)

    p1 = multiprocessing.Process(target=TestProc, args=(liste,results,))
    p1.start()
    p1.join()

    print(results)

When you do: 当您这样做时:

results[i] = ...

you are not adding values to the DictProxy at results[i] , you are replacing it with another brand new dict , inside the outer results dict . 您没有在results[i]上向DictProxy添加值, DictProxy在外部results dict中用另一个全新的dict替换了它。 Therefore, the only one you need to be synchronized is that outer dict . 因此,您唯一需要同步的就是外部dict

Replace line 15 with results = multiprocessing.Manager().dict() , and remove the loop line 18-20 that never did anything. results = multiprocessing.Manager().dict()替换第15行,并删除从未执行任何操作的循环行18-20。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM