This starmap example program works as intended:
import multiprocessing
def main():
pool = multiprocessing.Pool(10)
params = [ (2, 2), (4, 4), (6, 6) ]
pool.starmap(printSum, params)
# end function
def printSum(num1, num2):
print('in printSum')
mySum = num1 + num2
print('num1 = ' + str(num1) + ', num2 = ' + str(num2) + ', sum = ' + str(mySum))
# end function
if __name__ == '__main__':
main()
output:
in printSum
num1 = 2, num2 = 2, sum = 4
in printSum
num1 = 4, num2 = 4, sum = 8
in printSum
num1 = 6, num2 = 6, sum = 12
But if I change the starmap line to starmap_async like so:
pool.starmap_async(printSum, params)
and keep everything else the same I get no output at all !?!? From reading the docs https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool.map_async I'm unable to determine a reason for this. I've used starmap_async successfully in other situations. What am I missing here ??
--- Edit ---
I found that if in the above working program I change the line
pool.starmap(printSum, params)
to the following two lines:
result = pool.starmap_async(printSum, params)
result.get()
Then I get the expected result, so I suppose this at least solves the problem. But can somebody please explain why .get() is not necessary for the non-async version of map / starmap but .get() is necessary for the async versions ?? The really confusing thing is that for the async versions in some cases .get() is necessary (as above) but in other cases with the async version .get() is not necessary and I'm unable to determine how/why/when .get() is necessary other than through experimentation.