I have the following function and dictionary comprehension:
def function(name, params):
results = fits.open(name)
<do something more to results>
return results
dictionary = {name: function(name, params) for name in nameList}
and would like to parallelize this. Any simple way to do this?
In here I have seend that the multiprocessing
module can be used, but could not understand how to make it pass my results to my dictionary.
NOTE: If possible, please give an answer that can be applied to any function that returns a result.
NOTE 2: the is mainly manipulate the fits file and assigning the results to a class
UPDATE
So here's what worked for me in the end (from @code_onkel answer):
def function(name, params):
results = fits.open(name)
<do something more to results>
return results
def function_wrapper(args):
return function(*args)
params = [...,...,..., etc]
p = multiprocessing..Pool(processes=(max([2, mproc.cpu_count() // 10])))
args_generator = ((name, params) for name in names)
dictionary = dict(zip(names, p.map(function_wrapper, args_generator)))
using tqdm only worked partially since I could use my custom bar as tqdm reverts to a default bar with only the iterations.