Python Multi-thread queue

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.