Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why dict not being updated in python multiprocessing?

Please check below code.

I initialized a dict and send it to a function f. I checked the address of return_dict. It is not changing inside the process. So I thought the dict should be updated

But it is not updated, so why?

from multiprocessing import Process

return_dict = dict({})
print id(return_dict)


def f(value, return_dict):
    return_dict['value'] = value
    print return_dict


p = Process(target=f, args=(100, return_dict))
p.start()
p.join()
print return_dict
like image 902
Kramer Li Avatar asked Jul 12 '26 18:07

Kramer Li


1 Answers

Your Process() creates another process. It inherits objects as they are from your parent process when the child process is spawned, but modifications to these objects only modify the objects in child process memory and none of these changes are visible to parent.

You can overcome this by using Manager():

from multiprocessing import Process, Manager

def f(value, return_dict):
    return_dict['value'] = value
    print return_dict

d = Manager().dict()    
p = Process(target=f, args=(100, d))
p.start()
p.join()
print d
like image 70
Hannu Avatar answered Jul 14 '26 09:07

Hannu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!