I've recently been encountering a problem with Python's multiprocessing package that has me baffled. In a program, I have the following lines of code:
import multiprocessing as mp
...
def logResult(result):
results.append(result)
cpu = mp.cpu_count()
pool = mp.Pool(processes=cpu)
results = []
num = 400 # A tunable parameter
for x in range(0, num):
pool.apply_async(integrationFunction, args=(<some list of arguments...>), callback=logResult)
pool.close()
pool.join()
print(len(results)) # Unpredictable
...
I cant really reveal exactly what happens in integrationFunction
, but I know the following:
- It uses numpy.dblquad
at some point, and can be quite slow.
The problem is that in the last line of code, where I print the length of the results
list, I get an erratic result every time I run the program - it usually prints some number around 100-120 when num
is 400, but on odd occasions, it may print numbers outside of the range (and sometimes, even 0). But I expect it to print exactly num
(which is 400 in this example) - what might be some reasons this is occurring?
Thanks!