My original code was -
for j in tqdm(range(height), position = 0, leave = True):
y = y0 + j * ystep
for i in range(width):
x = x0 + i * xstep
#Do expensive task
To parallelize this I did this -
ht_iterator = range(height)
wt_iterator = range(width)
paramlist = list(itertools.product(ht_iterator, wt_iterator))
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.submit(self.process_single_pixel, paramlist)
Where self.process_single_pixel is basically the above for loop in the form of a function like so -
def process_single_pixel(params):
ht = params[0]; wt = params[1]
y = y0 + ht * ystep
x = x0 + wt * xstep
#do expensive task
#expensive task calls a recursive function to implement ray-tracing
In short I was earlier iterating over the entire height and width, instead I created an iterator with all possible combinations, so that it can be individually parallelized.
- Question : the program doesn’t complete execution and it seems like it gets stuck in an infinite loop. What am I doing wrong? I notice that the program never enters the function which needs to be executed and the computer hangs and becomes very slow. Also, what is the behavior for functions which do not explicitly return anything?
- I tried parallelization a dummy function -
with concurrent.futures.ProcessPoolExecutor() as executor:
ret = executor.map(self.dummy, paramlist)
for result in ret:
print(result)
def dummy(self, params):
return (params[0], params[1])
This gave an error BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending. which is apparently because these processes need to be guarded by __main__?