Unable to update nested dictionary value in multiprocessing's manager.dict()

后端 未结 2 2198
日久生厌
日久生厌 2021-02-15 18:15

I am trying to update a key in a nested dictionary of multiprocessing module\'s manager.dict() but not able to do so. It doesn\'t update the value and doesn\'t throw any error t

相关标签:
2条回答
  • 2021-02-15 18:36

    not sure why, but the Manager DictProxy object can't seem to handle mutating a nested part. this code works:

    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
    
    0 讨论(0)
  • 2021-02-15 18:47

    Looks like this issue remains per code below:

    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']);
    

    Output:

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

    Result of failed update 1: {'q': 'queue_1', 'ctlg': 'ctlg_1'}

    Result of failed update 2: {'q': 'queue_1', 'ctlg': 'ctlg_1'}

    Result of successful update 3: {'q': 'queue_1', 'ctlg': 'ctlg_1', 'name': 'concfun_1'}

    0 讨论(0)
提交回复
热议问题