0

I have a loop and in each iteration, there are tasks that should be executed in parallel. I need to wait for the tasks to run in parallel in the current iteration and then go to the next iteration.
For example,

a = [1,2,3,4,5,6,7,8,9,10]
pool = Pool(mp.cpu_count())

def fun1(x):
   ....

def fun2(x):
   ....

def fun3(x):
   ....

for x in a:
  pool.map(fun1, x)
  pool.map(fun2,x)
  pool.map(fun3,x)
  pool.close()
  pool.join()

Is this the right way ? Or how do I achieve this ?

2
  • Do you want fun1 to run for all x in a, then after it finishes you execute fun2 and fun3? Or do you want fun1,2,3 to run in parallel when x=1, then for x=2, and so on? Commented Jun 2, 2021 at 8:03
  • @EhabIbrahim I want fun1,2,3 to run in parallel when x=1, then for x=2, and so on. Commented Jun 3, 2021 at 9:04

1 Answer 1

3

Based on your comment, you would like to run fun1, fun2, fun3 in parallel for x=1, wait until they all finish, then move on to x=2 and repeat. This can be achieved like this:

import multiprocessing as mp
a = [1,2,3,4,5,6,7,8,9,10]

def fun1(x):
   ....

def fun2(x):
   ....

def fun3(x):
   ....

for x in a:
    # Create separate process for each function 
    p1 = mp.Process(target=fun1, args=(x))
    p2 = mp.Process(target=fun2, args=(x))
    p3 = mp.Process(target=fun3, args=(x))
    # Start all processes 
    p1.start()
    p2.start()
    p3.start()
    # Wait till they all finish and close them 
    p1.join()
    p2.join()
    p3.join()

Alternatively, if you would like to run fun1 for all x in a, the run fun2 then fun3, you can use a multiprocessing pool instead:

import multiprocessing as mp
a = [1,2,3,4,5,6,7,8,9,10]
pool = mp.Pool(mp.cpu_count())

def fun1(x):
   ....

def fun2(x):
   ....

def fun3(x):
   ....

# Run fun1 for all values in a
pool.map(fun1, a)
# Run fun2 for all values in a
pool.map(fun2, a)
# Run fun3 for all values in a
pool.map(fun3, a)
# Close pool 
pool.close()
pool.join()

In the multiprocessing pool case, pool.map(fun2, a) will not run unless pool.map(fun1, a) finishes running. For more information on Python's multiprocessing module, I highly recommend reading the documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.