Sunday, 20 December 2015

Python: apply_async and callback method example

apply_async(func[, args[, kwds[, callback[, error_callback]]]])
It is just like apply method; it will not block for the result and best suited to perform work in parallel.

Pool.apply_async method also has a callback, which, if supplied, is called when the function is complete. This can be used instead of calling get(). The order of the results is not guaranteed to be the same as the order of the calls to Pool.apply_async.


You can specify error_callback method also; this is called when function failed with an error. Both callback, error_callback accepts single argument.
from multiprocessing import Pool
from time import sleep
import random

def sum(task, a, b):
    sleepTime = random.randint(1, 4)
    print(task, " requires ", sleepTime, " seconds to finish")
    sleep(sleepTime)
    return a+b

def printResult(result):
    print(result)

if __name__=="__main__":
    myPool = Pool(5)

    result1 = myPool.apply_async(sum, args=("task1", 10, 20,), callback = printResult)
    result2 = myPool.apply_async(sum, args=("task2", 20, 30,), callback = printResult)
    result3 = myPool.apply_async(sum, args=("task3", 30, 40,), callback = printResult)
    result4 = myPool.apply_async(sum, args=("task4", 40, 50,), callback = printResult)
    result5 = myPool.apply_async(sum, args=("task5", 50, 60,), callback = printResult)

    print("Submitted tasks to pool")

    myPool.close()
    myPool.join()


Sample output
Submitted tasks to pool
task1  requires  2  seconds to finish
task2  requires  2  seconds to finish
task3  requires  4  seconds to finish
task4  requires  2  seconds to finish
task5  requires  1  seconds to finish
110
30
90
50
70





Previous                                                 Next                                                 Home

No comments:

Post a Comment