Consider the following:
def update_dict(d, k):
d[k] = True
This works as expected:
from functools import partial
my_dict = {}
_ = any(map(partial(update_dict, my_dict), range(5)))
print(my_dict)
# {0: True, 1: True, 2: True, 3: True, 4: True}
However, when using a multiprocessing.Pool
, the output is different:
from functools import partial
from multiprocessing import Pool
my_dict = {}
my_pool = Pool(processes=5)
_ = any(my_pool.imap(partial(update_dict, my_dict), range(5)))
print(my_dict)
# {}
It is as if my_dict
was never updated whatsoever, what is the reason for this?