Yancy
Yancy

Reputation: 1

Why Python doesn't compute in parallel when pipe() is introduced?

when I use multiprocessing to run two functions, I can get the result that they are run in parallel. But now I want the two subprocess to communicate with each other, so I introduced the Pipe() method, after which I found that this two fuctions were not run in parallel instead

(the project I attached cost 2s instead of 1s... if I delete the all the pipe() methods, it will cost only 1s, which means the two funcs run in parallel ).

I wonder what was wrong with my code.. is there something wrong when I use join() or recv() ??

In a word, I wanna know how to make two functions work in parallel when I use Pipe() to do some communication between them ? Thx a lot !

import numpy as np
import multiprocessing
import time 

def funca(mylist,conn):
        time.sleep(1)         
        mylist.append(666.6)
        conn.send(['a','a','a'])

def funcb(mylist,conn):
        time.sleep(1)
        mylist.append(66.6)
        conn.send(['b','b','b'])

if __name__ == "__main__":
    samples = [1,2,3]
    with multiprocessing.Manager() as MG: 
        conn1,conn2 = multiprocessing.Pipe()
        mylist = MG.list(samples)  
        tic = time.time()
        p1=multiprocessing.Process(target=funca,args=(mylist,conn1) ) 
        p1.start()
        print(conn2.recv())
        funcb(mylist,conn1)
#        p2=multiprocessing.Process(target=funcb,args=(mylist,conn1) ) 
#        p2.start()
        print(conn2.recv())
        p1.join()
#        p2.join()
        p1.terminate()
#        p2.terminate()
        print(list(mylist))
    toc = time.time()
    print('pass time = ',toc-tic)

Upvotes: 0

Views: 99

Answers (1)

Dan D.
Dan D.

Reputation: 74645

The issue is that you receive from the pipe print(conn2.recv()) before starting the second process. This causes the second process to be delayed until the first finishes.

This also occurs when the second process is ran in the main process as occurs in your example, confusing as it is with the commented out lines. Change:

print(conn2.recv())
funcb(mylist,conn1)

To:

funcb(mylist,conn1)
print(conn2.recv())

Upvotes: 1

Related Questions