How to get result from Pool.starmap_async()?

ElConrado picture ElConrado · Jun 5, 2019 · Viewed 11k times · Source

I have program which computes the index of array*value and returns a string. I use .starmap_async() because I must pass two arguments to my async function. The program looks as follows:

import multiprocessing as mp
from multiprocessing import freeze_support

def go_async(self, index, value) :
        return str(index * int(value))

def log_result(self, result):
        print("Succesfully get callback! With result: ", result)

def main() :
    array = [1,3,4,5,6,7]
    pool = mp.Pool() 
    res = pool.starmap_async(go_async, enumerate(array), callback = log_result)        
    print("Final result: ", res)
    pool.close()
    pool.join()

if __name__ == '__main__':    
    freeze_support()
    main()

I would like to get a result as an array of str:

res = ['0', '3', '8', '15', '24', '35']

but I have only result:

Final result: multiprocessing.pool.MapResult object at 0x000001F7C10E51D0

How to correctly get value from .starmap_async()? Moreover, callback does not raise.

Answer

Darkonaut picture Darkonaut · Jun 5, 2019

Pool's async-methods return objects which are conceptually "explicit futures", you need to call .get() to await and receive the actual result. So res.get() will give you your result. You also need to remove self from your functions, since you are not passing an instance along your starmap-call. This currently leads to an exception within your target function and that's also the reason why your callback doesn't fire.