2

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.

cdahms
  • 3,402
  • 10
  • 49
  • 75

1 Answers1

2

You don't await the result from the future you get with calling .starmap_async(), or do anything else what would prevent the MainProcess from exiting right after scheduling the job.

pool.starmap_async(printSum, params).wait() would do it, but it's pointless here. The async-variants make only sense if you need the calling thread unblocked to do something else in the meantime.

Darkonaut
  • 20,186
  • 7
  • 54
  • 65
  • But if I add wait(), doesn't that prevent execution from continuing and doing other stuff, which I'm under the impression is the whole point of having the _async version available vs the non-asynch version. I've found if I do pool.starmap_async(printSum, params).get() that seems to solve it, but I still don't understand what is going on completely. I just updated my question for this. – cdahms Jan 27 '20 at 17:57
  • @cdahms Your code doesn't show you doing something else or returning a result. If you need the result you would use `.get()`. They don't make a difference otherwise here. Run it multiple times, it depends on your OS how fast and when something will execute. – Darkonaut Jan 27 '20 at 18:03