Knock all night, leave a thought.
Found that it is similar to the C programming of Linux, is a Python syntax encapsulation.
I hope to have the opportunity to use it later.
A, multi-process functional implementation
ImportMultiprocessingImport Timedefworker_1 (interval): N= 5 whilen >0:#print (' The time is {0} '. Format (Time.ctime ())) Print 'Start worker_1'Time.sleep (interval) n-= 1Print 'End worker_1'defworker_2 (interval): N= 5 whilen >0:#print (' The time is {0} '. Format (Time.ctime ())) Print 'Start worker_2'Time.sleep (interval) n-= 1Print 'End Worker_2'defworker_3 (interval): N= 5 whilen >0:#print (' The time is {0} '. Format (Time.ctime ())) Print 'Start Worker_3'Time.sleep (interval) n-= 1Print 'End Worker_3'if __name__=='__main__': P1= multiprocessing. Process (target=worker_1, args= (1,)) P2= multiprocessing. Process (Target=worker_2, args= (2,)) P3= multiprocessing. Process (Target=worker_3, args= (3,)) P1.start () P2.start () P3.start ( )Print 'The number of the CPU is:'+Str (multiprocessing.cpu_count ()) forPinchMultiprocessing.active_children ():Print 'Child P.name:'+ P.name +'\tp.id'+Str (p.pid)Print 'END!!!!!!!!!!!'
B, multi-process class implementation
ImportMultiprocessingImport Timeclassclockprocess (multiprocessing. Process):def __init__(self, interval): multiprocessing. Process.__init__(self) self.interval=intervaldefRun (self): n= 5 whilen >0:Print 'The time is {0}'. Format (Time.ctime ()) time.sleep (self.interval) n-= 1if __name__=='__main__': P= Clockprocess (3) P.start ()
C, multi-process daemon
ImportMultiprocessingImport TimedefWorker (interval):Print 'worker Start:{0}'. Format (Time.ctime ()) Time.sleep (interval)Print 'worker End:{0}'. Format (Time.ctime ())if __name__=='__main__': P= multiprocessing. Process (Target=worker, args= (3,)) P.daemon=True P.start () p.join ()Print 'End'
D,lock
ImportMultiprocessingImportSYSdefWorker_with (lock, F): With Lock:fs= Open (F,'A +') n= 10 whilen > 1: Fs.write ('Lock acquired via with\n') n-= 1Fs.closedefWorker_no_with (lock, F): Lock.acquire ()Try: FS= Open (F,'A +') n= 10 whilen > 1: Fs.write ('Lock acquired directly\n') n-= 1fs.close ()finally: Lock.release ()if __name__=='__main__': Lock=multiprocessing. Lock () F='file.txt'W= multiprocessing. Process (Target=worker_with, args=(lock, f)) NW= multiprocessing. Process (Target=worker_no_with, args=(lock, F)) W.start () Nw.start ()Print 'End'
E. SEMAPHORE
ImportMultiprocessingImport Timedefworker (S, i): S.acquire ()Print(Multiprocessing.current_process (). Name +'Acquire') Time.sleep (i)Print(Multiprocessing.current_process (). Name +'Release') s.release ()if __name__=='__main__': S= multiprocessing. Semaphore (2) forIinchRange (5): PrintI P= multiprocessing. Process (Target=worker, args=(S, i)) P.start ()
F,event
ImportMultiprocessingImport Timedefwait_for_event (E):Print 'wait_for_event:starting'e.wait ()Print 'Wait_for_event:e.is_set ()'+Str (e.is_set ())defwait_for_event_timeout (E, T):Print 'wait_for_event_timeout:starting'e.wait (t)Print 'Wait_for_event_timeout:e.is_set ()'+Str (e.is_set ())if __name__=='__main__': E=multiprocessing. Event () W1= multiprocessing. Process (name='Block', Target=wait_for_event, args=(E,)) W2= multiprocessing. Process (name='Non-block', Target=wait_for_event_timeout, args= (e,2) ) W1.start () W2.start () Time.sleep (3) E.set ()Print 'Main:event is set'
G,pipe
ImportMultiprocessingImport TimedefProc1 (pipe): whileTrue: forIinchXrange (10): Print 'proc1 Send:%s'%(i) pipe.send (i) Time.sleep (1)defPROC2 (pipe): whileTrue:Print 'PROC2 Rev:', Pipe.recv () Time.sleep (1)if __name__=='__main__': Pipe=multiprocessing. Pipe () P1= multiprocessing. Process (Target=proc1, args=(Pipe[0],)) P2= multiprocessing. Process (TARGET=PROC2, args= (pipe[1],)) P1.start () P2.start () P1.join () P2.join ()
H. Queue
ImportMultiprocessingdefWriter_proc (q):Try: Q.put (1, block=False) Q.put (2, block=False) Q.put (3, block=False)except: PassdefReader_proc (q):Try: PrintQ.get (block=False)PrintQ.get (block=False)PrintQ.get (block=False)except: Passif __name__=='__main__': Q=multiprocessing. Queue () writer= multiprocessing. Process (Target=writer_proc, args=(q,)) Writer.start () Reader= multiprocessing. Process (Target=reader_proc, args=(q,)) Reader.start () Reader.join () Writer.join ()
Copy:
Http://www.cnblogs.com/kaituorensheng/p/4445418.html
There is also a pool that will be mended tomorrow.
Python multi-Process sample code