I am doing some calculations on large collections of bytes. The process runs on chunks of bytes. I am trying to use parallel processing using multiprocessing for performance enhancement. Initially I tried to use pool.map but that only allows single argument, then I found about pool.starmap. But pool.starmap gives results only when all the processes have finished. I want results as they come (sort of). I am trying to use pool.imap which does provide results as processes finish but does not allow multiple arguments (my function requires 2 arguments). Also, the sequence of result is important.
Some sample code below:
pool = mp.Pool(processes=4)
y = []
for x in pool.starmap(f, zip(da, repeat(db))):
y.append(x)
The above code works, but only gives the results once all the processes have completed. I cannot see any progress. This is why I tried to use pool.imap, works well but with only single argument:
pool = mp.Pool(processes=4)
y = []
for x in pool.imap(f, da)):
y.append(x)
On multiple arguments raises the following exception:
TypeError: f() missing 1 required positional argument: 'd'
Looking for simple way to achieve all 3 requirements:
Thanks!
I can answer the first two question pretty quickly. I think you should be able to handle the third question after understanding the first two.
1. Parallel Processing with Multiple Arguments
I'm not sure about the whole "starmap" equivalent but here's an alternative. What I've done in the past is condense my arguments into a single data object like a list. For example, if you want to pass three arguments to your map_function
, you could append those arguments into a list, and then use the list with the .map()
or .imap()
function.
def map_function(combo):
a = combo[0]
b = combo[1]
c = combo[2]
return a + b + c
if '__name__' == '__main__':
combo = []
combo[0] = arg_1
combo[1] = arg_2
combo[2] = arg_3
pool = Pool(processes=4)
pool.map(map_function, combo)
2. Tracking Progress
A good way to do this is using multiprocessing
's shared value. I actually asked this (almost) same exact question about a month ago. This allows you to manipulate the same variable from the different processes created by your map
function.