1. Obtain (logical processor number) of cores cpu
cpu_count()
import os res = os.cpu_count() print(res) # 4
2. The process pool
note:
Process and Pool differences:
Process: part of asynchronous concurrent program, the main process by default after waiting for the child process executed completely in the termination of the program
Pool: part of asynchronous parallel program, without any obstruction, the long primary process is finished, immediately terminator all children;
P = pool (. 6):
represent the same time allowing up to six parallel processes;
process parameters inside the cell pool
if pool without any parameters, the default os.cpu_count () to obtain the number of logic processor
if the rate of the parameters, it uses the current parameter values represent the same time the maximum number of allowed processes (parallel)
in the process pool, if the speed of a process to perform tasks too fast, taking more tasks will be executed, We will not create a new process
from multiprocessing Import Process, Pool Import os, Time, Random # between (1) the comparison process pool and process speed => faster process pool DEF FUNC (NUM): Print (os.getpid ()) Print (NUM) the time.sleep ( 0.1 ) for i in the Range (1000000 ): Pass IF __name__ == ' __main__ ' : # recording start time Startime = time.time () # 1. create a process pool the p-pool = (4 ) for i inRange (100 ): p.apply_async (FUNC, args = (I,)) # 3. Process pool p.close () # . After 4.udp message waiting loop execution ends child processes, then execute downwardly p .join () # end recording time endtime = time.time () Print ( " time is running No. 1: " , endtime-Startime) # 3.7787575721740723 Print ( " process pool run ended ... " ) # calculate prosess running time stratime = the time.time () LST = [] for I in Range (100 ): P = Process (target = FUNC, args = (I,)) p.start () lst.append (P) for I in LST: i.join () the endtime = the time.time () Print ( " No. 2 runs the event is: " , endtime-Startime) # 4.udp cycle message .919969081878662 Print ( " the end of the process run ... " )
2.2 apply synchronization program, you can get a direct return value in the child process (to understand)
from multiprocessing Import Process, Pool Import OS, Time, Random DEF Task (NUM): Print ( " NUM: Process number S% S% " % (NUM, os.getpid ())) return os.getpid () IF the __name__ = = ' __main__ ' : # the same time to run up to four parallel processes P = Pool (. 4 ) for I in Range (20 is ): RES = p.apply (Task, args = (I,)) Print ( " >>>> " , RES) Print ( " main program end " )
2.3apply_async asynchronous program, you can get the return value in the child directly get
from multiprocessing Import Process, Pool Import OS, Time, Random DEF Task (NUM): Print ( " NUM: Process number S% S% " % (NUM, os.getpid ())) return os.getpid () IF the __name__ = = ' __main__ ' : # the same time to run up to four parallel processes P = Pool (. 4 ) for I in Range (20 is ): RES = p.apply (Task, args = (I,)) Print ( " >>>> " , RES) Print ( " main program end " ) # (3) apply_async asynchronous program, you can get the child process the return value directly through GET DEF Task (NUM): the time.sleep (random.uniform ( 0.1, 1 )) Print ( " num:% s process number S% " % (NUM, os.getpid ())) return os.getpid () IF the __name__ == ' __main__ ' : LST = [] setvar = SET () P = Pool () for I in the Range (20 ): RESP.apply_async = (Task, args = (i,)) # <multiprocessing.pool.ApplyResult Object AT 0x7f821508fa58> lst.append (RES) Print (LST) for i in LST: # to get the current value of the child's return , get function itself contains obstruction, without close as you can and join RES = i.get () # get the value of the object, is the process ID Print (RES) setvar.add (RES) Print (setvar) # {37066, 37067 , 37068, 37069} # 3. Close the process pool p.close () # after 4.udp cycle message. all wait for the child to finish, and then execute down p.join () Print ( " Finish .... " )
2.4 map (map and use the same higher-order function, except that the concurrency and parallelism) Returns the list
from multiprocessing import Process,Pool import os,time,random def task(num): print("num:%s 进程号%s"%(num, os.getpid())) time.sleep(0.1) return num **2 if __name__ == '__main__': p = Pool() res = p.map(task,range(100)) print(res) print("主进程 finish")
2.5 close join and simultaneously, at the same time or not applicable, have a pair of
from multiprocessing Import Process, Pool Import os, Time, Random DEF Task (NUM): Print ( " NUM: Process ID% S S% " % (NUM, os.getpid ())) the time.sleep ( 0.1 ) return NUM * 2 * IF the __name__ == ' __main__ ' : LST = [] P = Pool () for I in Range (20 is ): # RES returns asynchronous process object RES = p.apply_async (Task, args = (I,) ) lst.append (RES) for i in LST: # at the same time get a return value, plus the blocking Print (i.get ()) # shutdown process pool # p.close () # # p.apply_async (Task, args = (100,)) # p.join () Print ( " main process Finish " )