pipe.py# multi-process data delivery receive and send (like socket)
From multiprocessing import process,pipedef F (conn): conn.send ([42,none, "Hello to Child"]) conn.send ( [All, None, "Hello from Child2"]) Print ("From parent:", CONN.RECV ()) If __name__ = = ' __main__ ': parent_conn, child_conn= Pipe () p=process (target =f,args= (Child_conn,)) P.start () print (PARENT_CONN.RECV ()) print (PARENT_CONN.RECV ()) Parent_ Conn.send ("Dog fat Bites you!") ") P.join ()
manger.py# data sharing and delivery across multiple processes (while modifying one copy of the data)
From multiprocessing import Process,managerimport osdef F (d,l): D[os.getpid ()]=os.getpid ( ) l.append ( Os.getpid ()) print (l) if __name__ = = "__main__": With Manager () as manager: d= manager.dict () # Generate a dictionary to share a single copy of data and pass in multiple processes L =manager.list (range (5)) #生成一个列表可在多个进程中共享一份数据和传递 p_list =[] for i in range (10): P =process (target=f,args= (d,l)) P.start () p_list.append (p) for res in p_list: res.join () print (d) print (L)
Multi-process-pipe and manager data sharing and delivery