I have this general function that I use to run many different operations that works to launch multiple processes and then keep track of the progress using the progressbar2 modules. Occasionally the progress bar results in a hang up and the function gets stuck. I can't seem to figure out why, if I remove the progress bar update, then the process finishes and moves on, so the problem seems to be in the while loop. Is there a better way to construct the while loop?
def runMultiProgress(function, inputList, cpus):
from progressbar import ProgressBar, Percentage, ETA
from time import sleep
#setup pool
p = multiprocessing.Pool(cpus)
#setup progress bar
widgets = [' Progress: ', Percentage(),' || ', ETA()]
pbar = ProgressBar(widgets=widgets, term_width=30, maxval=len(inputList)).start()
#setup results and split over cpus
results = []
r = [p.apply_async(function, (x,), callback=results.append) for x in inputList]
#refresh pbar every 5 seconds
while len(results) != len(inputList):
pbar.update(len(results))
sleep(5)
pbar.finish()
p.close()
p.join()
UPDATE: citing my sources, this multiprocessing + progress bar is from @julien-tourille answer here: Show the progress of a Python multiprocessing pool map call?