multiprocessing.Manager()。dict()无法更新第二级值

时间:2019-11-11 06:42:35

标签: python-3.x python-multiprocessing

我有一个dict x,格式如下:x_dic = {0:{'length':2,'current':0}},但是当我使用Manager()。dict()将x_dic传递给子进程时,我发现子进程无法更新“当前”中的值。

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()

2 个答案:

答案 0 :(得分:0)

您可以尝试使用multiprocessing.Process()

import multiprocessing as mp

m=mp.Manager()
x_dict=m.dict({0:{'length':2,'current':0}})

procs=[]
no_of_processes=2
for i in range(no_of_processes):
    p=mp.Process(target=func_name, args=(x_dict,)) #func_name takes one argument as x_dict and does all the manupulations as required
    p.start()
    procs.append(p)  #Just creating a pointer to the current process and storing in a list

for proc in procs:
    proc.join()

print(x_dict)

答案 1 :(得分:0)

当将python dict传递给Manager()。dict()时,不会更改python dict作为第二级。解决方案是将另一个Manager()。dict()作为第二级传递。例如:

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

x = {0:valuedic}
testdic.update(x)

然后在处理后成功更改valuedic。