1. Comparison of process and process pool efficiency
- Multi-process: P.start () process, just send a signal to the operating system, as to when to execute, is the operating system thing, when the operating system receives a signal, to help the process request a piece of memory space + copy the address space of the parent process
#Multi-Process Execution efficiency fromMultiprocessingImportProcessImport Timedeffunc (i): Sum=0 Time.sleep (1) Sum+=IPrint(sum)if __name__=='__main__': LS=[] Statt=time.time () forIinchRange (10): P= Process (target=func,args=((i,))) P.start () Ls.append (P) [P.join () forPinchLS]Print(Time.time ()-statt)#Execution Time1.3582112789154053
execution of multiple processes
- Process Pool:
A pool with a fixed number of processes inside. These processes are always on standby, and once a task comes, there is a process to deal with it immediately.
Because in the actual business, the task volume is much less, if the task volume is particularly much, it is impossible to open the corresponding number of processes
Opening so many processes first requires a lot of time for the operating system to manage it for you. Second, it takes a lot of time to get
The CPU helps you dispatch it.
The process pool also helps programmers manage processes in the pool .
#the efficiency of the process pool fromConcurrent.futuresImportProcesspoolexecutor fromMultiprocessingImportProcessImport Timedeffunc (i): Sum=0 Time.sleep (1) Sum+=Ireturnsumls= []if __name__=='__main__': P= Processpoolexecutor (4) Start=time.time () forIinchRange (10): #drop a task into the process pool, these 10 sent signals are added directly to the list #as far as you do not execute, that is the operating system, then there are 10 return value objects in the listobj =p.submit (func,i) ls.append (obj)#the Task object is sorted in order in the LS list, and then P.result () waits for the result in turn![Print(P.result ()) forPinchLS]Print(Time.time ()-start)#The above list waits, this will be done in a flash, because each task in the code above has been executed .P.shutdown (wait=True)#Time Efficiency3.124636650085449
time efficiency of the process pool
- Summarize:
If the number of processes in the machine can withstand the more open, the faster the execution efficiency!
1. Lightweight task multi-process and process pool execution is not very efficient; Because the time to open the process when the multi-process is open
And CPU switching time can be negligible!
2. When the task volume is large, because the process pool already has the open process, can be scheduled at any time, saving the time of multi-process open process
and resource Utilization!
2. Threading vs. Process efficiency
Process and thread and coprocessor efficiency comparison