无法在多处理的manager.dict()中更新嵌套字典值

时间:2016-05-29 12:46:08

标签: python dictionary multiprocessing python-multiprocessing multiprocessing-manager

我正在尝试更新多处理模块的manager.dict()的嵌套字典中的键,但不能这样做。它不会更新值,也不会抛出任何错误。

代码:

import time
import random
from multiprocessing import Pool, Manager

def spammer_task(d, token, repeat):
    success = 0
    fail = 0
    while success+fail<repeat:
        time.sleep(random.random()*2.0)
        if (random.random()*100)>98.0:
            fail+=1
        else:
            success+=1
        d[token] = {
            'status': 'ongoing',
            'fail': fail,
            'success': success,
            'repeat': repeat
        }
    print d[token]['status']
    d[token]['status'] = 'complete'
    return

p = Pool()
m = Manager()
d = m.dict()

p.apply_async(spammer_task (d, 'abc', 5))
print d

输出:

  

持续

     

{&#39; abc&#39;:{&#39;状态&#39;:&#39;正在进行&#39;,&#39;失败&#39;:0,&#39;重复&#39 ;:5,&#39;成功&#39;:5}}

我的期望是,一旦循环结束,它应该使d [&#39; abc&#39;] [&#39; status&#39;] =完成。但在最终印刷品上,它打印的状态为“正在进行中”。仅

2 个答案:

答案 0 :(得分:5)

不确定原因,但是管理器DictProxy对象似乎无法处理变异嵌套部分。这段代码有效:

import time
import random
from multiprocessing import Pool, Manager

def spammer_task(d, token, repeat):
    success = 0
    fail = 0
    while success+fail<repeat:
        time.sleep(random.random()*2.0)
        if (random.random()*100)>98.0:
            fail+=1
        else:
            success+=1
        d[token] = {
            'status': 'ongoing',
            'fail': fail,
            'success': success,
            'repeat': repeat,
        }
    print d[token]['status']
    foo = d[token]
    foo['status'] = 'complete'
    d[token] = foo
    return

p = Pool()
m = Manager()
d = m.dict()

p.apply_async(spammer_task(d, 'abc', 5))
print d

答案 1 :(得分:0)

以下代码看起来仍然存在此问题:

import multiprocessing, sys;

if __name__ == '__main__':

print(sys.version);

mpd = multiprocessing.Manager().dict();
mpd['prcss'] = {'q' : 'queue_1', 'ctlg' : 'ctlg_1' };

# update 1 - doesn't work!
mpd['prcss'].update( { 'name': 'concfun_1'} );
print('Result of failed update 1:', mpd['prcss']);

# update 2 - doesn't work!
mpd['prcss']['name'] = 'concfun_1';
print('Result of failed update 2:', mpd['prcss']);

# update 3 - works!
mpd_prcss = mpd['prcss'];
mpd_prcss['name'] = 'concfun_1';
mpd['prcss'] = mpd_prcss;
print('Result of successful update 3:', mpd['prcss']);

输出:

  

3.6.1(v3.6.1:69c0db5,2017年3月21日,17:54:52)[MSC v.1900 32 bit(Intel)]

     

更新失败的结果1:{&#39; q&#39;:&#39; queue_1&#39;,&#39; ctlg&#39;:&#39; ctlg_1&#39;}

     

更新失败的结果2:{&#39; q&#39;:&#39; queue_1&#39;,&#39; ctlg&#39;:&#39; ctlg_1&#39;}

     

成功更新的结果3:{&#39; q&#39;:&#39; queue_1&#39;,&#39; ctlg&#39;:&#39; ctlg_1&#39;,   &#39;姓名&#39;:&#39; concfun_1&#39;}