python - Parallelizing modifications to a dictionary -
i have dictionary my_dict containing lists, , iterable keys lot of keys run function on:
for key in keys: if key in my_dict: my_dict[key].append(my_fun(key, params)) else: my_dict[key] = [my_fun(key, params)] my_fun slow. how parallellize loop?
is just:
import multiprocessing def _process_key(key): if key in my_dict: my_dict[key].append(my_fun(key, params)) else: my_dict[key] = [my_fun(key, params)] if __name__ == '__main__': pool(5) p: p.map(_process_key, keys)
the dict in parent memory space need update there. pool.map iterates through whatever returned worker function, have return in useful form. collections.defaultdict helper creates items you, can
import multiprocessing import collections def _process_key(key): return key, my_fun(key, params) if __name__ == '__main__': pool(5) p: my_dict = collections.defaultdict(list) key, val in p.map(_process_key, keys): my_dict[key].append(val)
Comments
Post a Comment