from multiprocessing import Process,Pipe import os,time #建立管道對象 #當參數爲False的時候child只能recv parent只能send # child_conn,parent_conn = Pipe(False) child_conn,parent_conn = Pipe() #子進程函數 def fun(name): time.sleep(1) #發送一個字符串到管道 child_conn.send('hello' + str(name)) print(os.getppid(),"----",os.getpid()) jobs = [] #建立5個子進程 for i in range(5): p = Process(target = fun,args = (i,)) jobs.append(p) p.start() for i in range(5): data = parent_conn.recv() print(data) for i in jobs: i.join()