0

I am trying to execute below code in parallel using multiprocessing but I am not getting proper output.

Iteration with 1 and iteration with 5 should happen in parallel

from multiprocessing import Pool
def func1(x):
    i = x
    while i < 10:
        print(str(i)+" fun1")
        i +=1

if __name__ == '__main__':
    pool = Pool(processes=1) 
    pool.map(func1, [1,5])

Result I got: happening more iterations

1 fun1 2 fun1 3 fun1 4 fun1 5 fun1 6 fun1 7 fun1 8 fun1 9 fun1 5 fun1 6 fun1 7 fun1 8 fun1 9 fun1

Expected output: both iterations should happen in parallel

1 fun1 5 fun1 2 fun1 6 fun1 3 fun1 7 fun1 4 fun1 8 fun1 5 fun1 9 fun1 6 fun1 7 fun1 8 fun1 9 fun1

1
  • i updated the question please have a look Commented Dec 15, 2018 at 16:31

2 Answers 2

1

I can't reproduce your result with your code. I'm getting the expected behavior (note the call of 'func1' before the 'if' statement):

1 fun1 2 fun1 3 fun1 4 fun1 5 fun1 6 fun1 7 fun1 8 fun1 9 fun1 1 fun1 2 fun1 3 fun1 4 fun1 5 fun1 6 fun1 7 fun1 8 fun1 9 fun1 5 fun1 6 fun1 7 fun1 8 fun1 9 fun1

For some reason, you're calling func1(1) three times.

In any case, you start 1 process only, so the execution will be serial. You need more than one process to get parallel execution.

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

2 Comments

i have updated the code can you please have a look...i used 2 process but still same result
are you expecting an alternate printing? If so, it is very unlikely if you do not force the processes to wait the others.
1

What you are expecting is not happening due to the Global Interpreter Lock. Give it a look: https://wiki.python.org/moin/GlobalInterpreterLock

1 Comment

multiprocessing works around that by using multiple processes, each of which has its own lock. I'd say the processes=1 is what restricts this code, as well as the simple fact the job is so short one may finish before the other is told what to do. Thirdly, the job isn't productive; map would normally collect the return values.

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.