'multiproessing using apply and pass array as argument for each process

consider the case that each process need to do somthing on an array. It seemes Pool.apply is the right choice for this job.

def sumj(i, arr): 
    print(i, os.getpid())

    sleep(0.5)
    return np.sum(arr)


if __name__ == "__main__":

    mat = np.ones((40, 10))
    pool = Pool(processes=10)
    results = [pool.apply(sumj, args=(i, mat[i,:])) for i in range(40)]
0 1220757
1 1220758
2 1220759
3 1220760
4 1220761
5 1220762
6 1220763

why am I getting a serial running, pid changes but each 0.5 sec I get one row of printed data ?



Solution 1:[1]

LINK

apply(func[, args[, kwds]]) Call func with arguments args and keyword arguments kwds. It blocks until the result is ready. Given this blocks, apply_async() is better suited for performing work in parallel. Additionally, func is only executed in one of the workers of the pool.

pool = Pool(processes=10)
results = [pool.apply_async(sumj, args=(i, mat[i,:])) for i in range(40)]
print([i.get() for i in results])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abolfazl