Process join a method of object
During the primary process if you want to run concurrently perform other tasks, we can open a child process, the child process tasks and task at this time of the main process of the two cases
Case 1:
In the sub-process tasks and task master process independently of each other, the main task after the first process is finished, the main process need to wait for the child process is finished, then uniform recycling resources. This is not join method
Case 2:
If the primary process tasks in the implementation to a certain stage, the need to wait for the child process is finished you can proceed ,
You need to have a mechanism that allows the main process to detect whether a child process completes, execution continues after the child process is finished, or has been blocked in place, this is the role of the join method
Waiting to get the main course, after all the child process is finished, the main process has to be continued
from multiprocessing import Process import time import os def task(): print("%s is running,parent id is <%s>" % (os.getpid(), os.getppid())) time.sleep(3) print("%s is done,parent id is <%s>" % (os.getpid(), os.getppid())) if __name__ == "__main__": t = Process(target=task, ) t.start() t.join() # Child process and other main processes executed over Print ( " master " , os.getpid (), os.getppid ()) ''' 24880 is running,parent id is <25956> 24880 is done,parent id is <25956> Main 259,562,992 '''
Child process runs out, the last print main process, the main end of the process all the zombies will recover
Open multiple word process sends a signal to the operating system, but too much multitasking operating systems to process, which first opened the child process is random, and sometimes may first open the main process first,
When open operating system, open how long, we do not know
from multiprocessing import Process import time import os def task(name): print('%s is running' %name) time.sleep(2) print('%s is end' %name) if __name__ == '__main__': P1 = Process (target = Task, args = ( ' subprocess 1 ' ,)) P2 = Process (target = Task, args = ( ' sub-process 2 ' ,)) P3 = Process (target = Task, args = ( ' sub-process 3 ' ,)) P4 = Process (target = Task, args = ( ' child process ' ,)) p1.start() p2.start() p3.start() p4.start() print ( " 主' , os.getpid () os.getppid ()) ''' The child process 1 is running The child process 2 is running Lord 92,685,236 3 is running a child process 4 is running a child process The child process 1 is end 2 is end the child process 3 is end the child process 4 is end the child process '''
It is also possible so, first open the main process,
Lord 95,565,236 child process 1 IS running The child process 3 IS running The child process 2 IS running Child process 4 IS running The child process 1 IS End The child process 3 IS End The child process 2 IS End Child process 4 IS End
p.start () sends a signal only to the operating system
Jo in will become serial?
Since the join is waiting for the end of the process, then I write like this, then he is a serial process does not it?
Of course not, must be clear: p.join () is who should be allowed, etc.?
Obviously p.join () is the main thread to wait for the end of the p sub-process, the main process is stuck in no way a child process p,
from multiprocessing import Process import time import os def task(name): print('%s is running' %(name)) time.sleep(2) print('%s is end' %(name)) if __name__ == '__main__': P1 = Process (target = Task, args = ( ' subprocess 1 ' ,)) P2 = Process (target = Task, args = ( ' sub-process 2 ' ,)) P3 = Process (target = Task, args = ( ' sub-process 3 ' ,)) P4 = Process (target = Task, args = ( ' child process ' ,)) p1.start() p2.start() p3.start() p4.start() p1.join() p2.join() p3.join() p4.join() print ( " 主' , os.getpid () os.getppid ())
Detailed analysis as follows:
As long as the process will start at the beginning of the run, so when p1-p4.start (), the system has a process of four concurrent
And we p1.join () is waiting for the end of p1, p1 right does not end as long as the main thread would have been stuck in place, which is the crux of the problem
is to join the main thread, etc., and p1-p4 is still executed concurrently, p1.join when the rest p2, p3, p4 is still running, such as the end of the # p1.join, may p2, p3, p4 already ended such p2.join, p3.join.p4.join detected directly, without waiting
So the total time it takes to join 4 is still the longest time-consuming processes running
So it will not be executed serially executed concurrently
The total time it takes to join 4 is still the longest time-consuming processes running
So is 5 seconds, 1 child process is that the waiting time
from multiprocessing import Process import time import os def task(name,n): print('%s is running' %(name)) time.sleep(n) print('%s is end' %(name)) if __name__ == '__main__': start = time.time() P1 = Process (target = Task, args = ( ' subprocess 1 ' , 5 )) P2 = Process (target = Task, args = ( ' sub-process 2 ' , 2 )) P3 = Process (target = Task, args = ( ' sub-process 3 ' , 2 )) P4 = Process (target = Task, args = ( ' child process ' , 2 )) p1.start() p2.start() p3.start() p4.start() p1.join() p2.join() p3.join() p4.join() print('主',time.time() - start) ''' The child process 1 is running The child process 2 is running 3 is running a child process 4 is running a child process 2 is end the child process 3 is end the child process 4 is end the child process The child process 1 is end Main 5.413309812545776 '''
This serial approach is
like a sub-process executed when there is no sub-process 2 transmits a signal to other sub-process 1 executed, and then the transmission signal sub-process 2, then opener process 2 performed in the order such
from multiprocessing import Process import time import os def task(name,n): print('%s is running' %(name)) time.sleep(n) print('%s is end' %(name)) if __name__ == '__main__': start = time.time() P1 = Process (target = Task, args = ( ' subprocess 1 ' , 5 )) P2 = Process (target = Task, args = ( ' sub-process 2 ' , 2 )) P3 = Process (target = Task, args = ( ' sub-process 3 ' , 2 )) P4 = Process (target = Task, args = ( ' child process ' , 2 )) p1.start() p1.join() p2.start() p2.join() p3.start() p3.join() p4.start() p4.join() print('主',time.time() - start) ''' The child process 1 is running The child process 1 is end The child process 2 is running 2 is end the child process 3 is running a child process 3 is end the child process 4 is running a child process 4 is end the child process Main 12.212698698043823 '''
Above the boot process and join the process can be shortened to less
from multiprocessing import Process import time import os def task(name,n): print('%s is running' %(name)) time.sleep(n) print('%s is end' %(name)) if __name__ == '__main__': start = time.time() P1 = Process (target = Task, args = ( ' subprocess 1 ' , 5 )) P2 = Process (target = Task, args = ( ' sub-process 2 ' , 2 )) P3 = Process (target = Task, args = ( ' sub-process 3 ' , 2 )) P4 = Process (target = Task, args = ( ' child process ' , 2 )) process_list = [p1,p2,p3,p4] for p in process_list: p.start() for p in process_list: p.join() print('主',time.time() - start)
join ensure that all children complete primary execution process to work, or has been blocked