The main method used is the from multiprocessing import Pool
Map () synchronization
Apply () Sync
Apply_async () asynchronous manual Close () join () the gradual and progressive of the learning
#!/usr/bin/env python#!--*--coding:utf-8--*--#[email protected]: 2018/7/18 16:44#[email protected] Truenewbee#Why there is a concept of process pooling#Efficiency#each open process opens the memory space that belongs to this process#Register Stack file#scheduling of excessive processes operating systems#Process Pool#Create a pool in Python that belongs to the process first#This pond specifies how many processes can be placed .#These processes are created first.#more advanced Process Pools#can change the number of processes based on user needs#comes with a join method, which is asynchronous#the parameters passed in map (Func,range) can only be passed to an iteration range, list, dictionary, etc.Import Time fromMultiprocessingImportPool, Processdeffunc (n): forAinchRange (10): Print(n+1)if __name__=='__main__': Start=time.time () pool= Pool (5)#5 ProcessesPool.map (func, Range (100))#100 Tasks AsynchronousT1 = Time.time ()-Start P_list= [] forIinchRange (100): P= Process (Target=func, args=(i,)) P_list.append (P) p.start () forIinchp_list:p.join () T2= Time.time ()-StartPrint(T1, T2)
#!/usr/bin/env python#!--*--coding:utf-8--*--#[email protected]: 2018/7/18 20:29#[email protected] Truenewbee#the Apply () method is a synchronous#The Apply_async () method is asynchronous and generally uses thisImport TimeImportOS fromMultiprocessingImportPooldeffunc (n):Print('Start func%s'%N, Os.getpid ()) Time.sleep (1) Print('End func%s'%N, Os.getpid ())if __name__=='__main__': P= Pool (5) forIinchRange (10): #p.apply (func, args= (i,)) # This method is synchronousP.apply_async (func, args= (i,))#the method is asynchronous. #two words that must be added to the use of Apply_asyncP.close ()#End Process Receive taskP.join ()#the end of task execution in the perceptual process pool
#!/usr/bin/env python#!--*--coding:utf-8--*--#[email protected]: 2018/7/18 21:05#[email protected] Truenewbee#p = Pool ()#P.map (FuncName, iterable) performs tasks asynchronously by default and comes with close and join#p.apply synchronous calls.#P.apply_async asynchronous calls and the main process completely asynchronously require manual close and joinImport Time fromMultiprocessingImportPooldeffunc (I1): Time.sleep (0.5) returnI1*I1if __name__=='__main__': P=Pool ()#res_list = [] # Save Res object to the next piece to be received #For I in Range (Ten): #res = P.apply_async (func, args= (i,)) # Apply_async result is the return value of func #res_list.append (RES) ## Res.get () # get () is waiting for the result of Func's calculation, blocking (synchronous) #For Res in res_list: #print (Res.get ()) # every five returns data so get () becomes asynchronous #map ()ret = P.map (func, Range (100)) Print(ret)#overall return all data
#!/usr/bin/env python#!--*--coding:utf-8--*--#[email protected]: 2018/7/18 21:06#[email protected] Truenewbee#callback functionImportOS fromMultiprocessingImportPooldeffunc1 (n):Print('In func1', Os.getpid ())returnNNdefFunc2 (NN):Print('In Func2', Os.getpid ())Print(NN)if __name__=='__main__': Print('Main process:', Os.getpid ())#The callback function executes in the main process.p = Pool (5) forIinchRange (10): P.apply_async (func1, args= (ten,), CALLBACK=FUNC2)#callback FUNC2 return value programming parameter passed to FUNC1p.close () p.join ( )
7.18python Process Pool