#Communication between Processes#1.Queue#It 's similar to the line thread's queue, but different. fromMultiprocessingImportProcess,queueImportOSdefF (q, N): Q.put ([11, N, None]) Print('Subpro', ID (q))if __name__=='__main__': Q=Queue ()Print('Mainpro', ID (q)) LST=[] forIinchRange (3): P=process (target=f,args= (q,i))#Q must be passed in because memory is not shared between different processeslst.append (P) p.start ()Print(Q.get ())Print(Q.get ())Print(Q.get ()) forIinchLst:i.join ()#The memory address of the main process and other sub-processes Q (queue) is different, stating that there is not the same Q,python internal operation,#It is possible that the pickle operation occurs between processes when the queue is passed.
#2.Pipes fromMultiprocessingImportProcess,pipedefF (conn): Conn.send ([1,'AAA']) conn.close ()if __name__=='__main__': #returns the parent process connection and the child process connectionparent_conn,child_conn=Pipe () p=process (target=f,args= (Child_conn,))#parameters must be added () Don't forget, also, don't forgetP.start ()Print(Parent_conn.recv ())#Unlike a socket, recv () cannot add a data size parameter, #transferring data is not necessarily bytesP.join ()
Python Multi-thread queue