1. Can the process be opened indefinitely?
The process cannot be opened indefinitely, the CPU has 16 cores, and the delegate can handle 16 tasks at the same time, if there are 160 processes, each process
Processing 160 processes with a processor that requires a 0.01s,16 core requires 0.5s, because the more processes it takes, the more time is needed.
2, Process Pool
ImportOSImport TimeImportRandom fromMultiprocessingImportPool fromMultiprocessingImportProcessdeffunc (i): I+ = 1if __name__=='__main__': P= Pool (5)#5 processes were created. In general, the value here is the number of #+1, if you do not know the number of cores of the CPU, you can call Os.cpu_count () to viewStart =time.time () p.map (Func,range (1000))#target = func Args=next (iterable) # [(), 1,2,3,4]P.close ()#is not allowed to add tasks to the process pool againP.join ()Print(Time.time ()-start) Start=time.time () L= [] forIinchRange (1000): P= Process (target=func,args= (i,))#100 Processes createdP.start () l.append (P) [I.join () forIinchL]Print(Time.time ()-start)
3,
Import Time fromMultiprocessingImportPool#Applydeffunc (i): Time.sleep (1) I+ = 1#print (i) returnI+1if __name__=='__main__': P= Pool (5) res_l= [] forIinchRange (20): #p.apply (func,args= (i)) # Apply is a mechanism for synchronizing commit tasksres = P.apply_async (func,args= (i,))#Apply_async is the mechanism for submitting tasks asynchronouslyres_l.append (RES)#print (Res.get ()) # Blocking: Waiting for task resultsP.close ()#close must be added to the join, and no new tasks are allowed.P.join ()#wait for the child process to end and then execute down[Print(I.get ()) forIinchRes_l]
Python: Process Pool