I am trying to parallelize a for loop in python that must execute a function with several arguments, one of which will be changing through the loop. The loop itself needs to be embedded in a function. I have already looked here, here and here in stackoverflow and beyond (here and here) but I just cannot make it work :(
Below is a MWE:
import time
import numpy as np
from multiprocessing import Pool
from functools import partial
def mytestFun(otherStuff, myparams):
return myparams[0]*otherStuff - myparams[1]
def myfun1(extraParams, mylist):
[myMat, otherStuff] = extraParams
for ivals in mylist:
myparams = myMat[ivals,:]
result = mytestFun(otherStuff, myparams)
return result
if __name__ == '__main__':
a_list = [0, 1, 2, 3, 4, 5]
myMat = np.random.uniform(0,1,(6,2))
extraParams = [myMat, 5]
print(myfun1(extraParams, a_list))
pool = Pool()
func = partial(myfun1, extraParams)
pool.map(func, a_list)
pool.close()
pool.join()
And I keep getting errors that I don't know how to interpret:
Traceback (most recent call last):
File "exampleMultiProcessing.py", line 61, in <module>
pool.map(func, a_list)
File "/Users/laurama/miniconda3/lib/python3.7/multiprocessing/pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/Users/laurama/miniconda3/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
TypeError: cannot unpack non-iterable int object
Thanks in advance!