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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With