1. Creation of processes
frommultiprocessing Import processimport time,osdef Hi (name): Time.sleep (3) Print ("Hello%s"%name,time.ctime ()) Print ("The process number is", Os.getpid ())if__name__=="__main__": forIinchRange3): T= Process (target=hi,args= ("Meng Meng",)) T.start () print ("main process Number:", Os.getpid ()) Print ("End All")View Code
2. Obstacles to progress
frommultiprocessing Import processimport time,osdef Hello (name): Time.sleep (2) Print ("Hello%s"%name,time.ctime ()) Print ("The process number is", Os.getpid ())if__name__=="__main__": L=[] forIinchRange3): T= Process (target=hello,args= ("Dream Dream",)) T.start () l.append (t) # T.join () # T.join () # block the main process forTinchL:t.join () print ("main process Number:", Os.getpid ()) Print ("End All")View Code
2.2. Inherited calls to processes
fromMultiprocessingImportProcessImportTime,osclassmyprocess (Process):#def __init__ (self,name): #Super (Myprocess,self). __init__ () #self.name =name defRun (self): Time.sleep (1) Print("Hello", Self.name,time.ctime ())if __name__=='__main__': P_list=[] forIinchRange (3): #P =myprocess ("Zhang San")p =myprocess () P.start () P_list.append (p)#P.join () forPinchP_list:p.join ()Print("End")View Code
3. The daemon of the process
#___ ' Daemon: Process Daemon, sub-process follows main process to retreat ' 'classmyprocess:defMusic1 (self):Print("begin to listen%s"%time.ctime ()) Time.sleep (2) Print("stop to listen%s"%time.ctime ())defgame1 (self):Print("begin to game%s"%time.ctime ()) Time.sleep (3) Print("stop to game%s"%time.ctime ())if __name__=="__main__": Duixiang=myprocess () P1= Process (target=duixiang.music1) P2= Process (target=duixiang.game1) P2.daemon=True L=[] L.append (p1) l.append (p2) forIinchL:i.start ()#I.join () Print("All over end ... .")
4. Process Communication Queue-queue
fromMultiprocessingImportProcess,queueImport Timedeffoo (q): Time.sleep (1) Q.put (6776) q.put ({"name":"Zhang San"})if __name__=='__main__': Q=Queue ()#q=multiprocessing. Queue () #Create a process queueP=process (target=foo,args=(q,)) P.start ()Print(Q.get ())Print(Q.get_nowait ())Print(Q.get (Block=false))
5. Process Communication-piping pipe
fromMultiprocessingImportProcess, PipeImport TimeImportOSdefF (conn): Conn.send ([12,{"name":"Nutcharin"},"Hello"]) Date=conn.recv ()Print("the message from my father was ,", Date,time.ctime ()) Conn.close ()Print("Child process number%s, the child process gets the data ID%s:"%(Os.getpid (), ID (conn)))if __name__=='__main__': Parent_conn,child_conn=Pipe () p=process (target=f,args=(Child_conn,)) P.start () Sunsend=parent_conn.recv ()Print("the message from my son is:", Sunsend,time.ctime ()) Parent_conn.send ("Hello, son.") Parent_conn.close ()Print("parent Process Process number%s, the parent process gets the data ID%s:"%(Os.getpid (), ID (parent_conn))) P.join ()
6. Process Synchronization
" "the locked part is completely serial." " fromMultiprocessingImportProcess,lockImport Timedeff (l,i): L.acquire () time.sleep (1) Print("Hello", i) l.release ()if __name__=='__main__': Lock=Lock () forNuminchRange (10): Process (Target=f,args= (Lock,num)). Start ()
7. Process Pool
deff (i): Time.sleep (1) Print(i)if __name__=='__main__': Pool=pool (5)#the default number of parameters is run as CPU forIinchRange (100): #pool.apply (func=f,args= (i)) # Synchronization InterfacePool.apply_async (func=f,args=(i,)) Pool.close () Pool.join ( )#the Process pool format is first close after join, the call order is fixed Print(" over all")
Python Multi-process