Search code examples
python-3.xpython-multiprocessing

multiprocessing.Manager().dict() can't update second level value


i have a dict x ,format like: x_dic = {0:{'length':2,'current':0}},but when i use Manager().dict() to pass x_dic to child process, i found the value in 'current' can't update by child process.

method 1: 
dic[i]['current'] += 1

method 2:
current_val = dic[i]['current']
current_val += 1
dic[i]['current'] = current_val
if __name__ == '__main__':
    # set config of logger
    print("{}:{}:{}".format(time.localtime().tm_hour, 
          time.localtime().tm_min, time.localtime().tm_sec))
    print(os.getpid())

    # set parameter
    lock = multiprocessing.Lock()
    pool = multiprocessing.Pool(processes=2, initializer=start_process)

    # set test dic 
    testdic = multiprocessing.Manager().dict()
    x = {0:{'length':2,'current':0}}
    testdic.update(x)

    # before multi
    print('now value testdic',dict(testdic))

    # running
    partialmulti = partial(multi_core, testdic=testdic)
    for i, _ in enumerate(pool.imap_unordered(partialmulti,[0,0,0])):
        print('finish process: ',i)

    pool.close()
    pool.join()

    # after multiprocessing
    print('after multi',dict(testdic))

    pool.terminate()

Solution

  • when pass a python dict to Manager().dict(), python dict as the second level won't be changed. The solution is to pass anther Manager().dict() as the second level. For example:

    valuedic = multiprocessing.Manager().dict()
    valuedic.update({'length':0,'current':1})
    
    x = {0:valuedic}
    testdic.update(x)
    

    then the valuedic will be successfully changed after multiprocessing.