import numpy as np import multiprocessing as mp ar = np.zeros((5,5)) def callback_function(result): x,y,data = result ar[x,y] = data def worker(num): data = ar[num,num]+3 return num, num, data def apply_async_with_callback(): pool = mp.Pool(processes=5) for i in range(5): pool.apply_async(worker, args = (i, ), callback = callback_function) pool.close() pool.join() print "Multiprocessing done!" if __name__ == '__main__': ar = np.ones((5,5))
Explanation: You have configured your data array, as well as your workers and callback functions. The number of processes in the pool has created a number of independent workers, where each employee can perform more than one task. The callback returns the result back to the array.
__name__=='__main__' protects the next line from starting with each import.
source share