10

我正在尝试更新多处理模块 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

输出:

进行中

{'abc': {'status': 'ongoing', 'fail': 0, 'repeat': 5, 'success': 5}}

我的期望是,一旦 while 循环结束,它应该使 d['abc']['status'] = 完成。但在最终打印时,它仅将其状态打印为“进行中”。

4

2 回答 2

13

不知道为什么,但 Manager 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
于 2016-05-29T13:24:19.267 回答
4

看起来这个问题仍然存在于下面的代码中:

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 位(英特尔)]

更新 1 失败的结果:{'q': 'queue_1', 'ctlg': 'ctlg_1'}

更新 2 失败的结果:{'q': 'queue_1', 'ctlg': 'ctlg_1'}

成功更新 3 的结果:{'q': 'queue_1', 'ctlg': 'ctlg_1', 'name': 'concfun_1'}

于 2018-02-06T15:11:57.417 回答