from multiprocessing Import Process Import os DEF func1 (): Print ( ' sub-process 1 ' , os.getpid ()) # child process: Get the current process of process ID Print ( ' the parent process child process: ' , os.getppid ( )) # get the process of the parent process the above mentioned id DEF func2 (): Print ( ' sub-process 2 ' , os.getpid ()) Print ( ' the parent process child process: ' , os.getppid ()) DEF func3 (): Print ( ' sub-process 3 ', os.getpid ()) Print ( ' the parent process child process: ' , os.getppid ()) IF __name__ == ' __main__ ' : p1 = Process (target = func1) # Registration: The name of the function (memory address) Sign into child processes P2 = process (target = func2) # p1 is a process object p3 = process (target = func3) p1.start () # open a child process p2.start () # three parallel sub-processes p3 .start () Print ( ' parent: ' , os.getpid ()) # parent: get the process ID of the current process Print ( ': Parent process of the parent process ' , os.getppid ()) # parent process id acquisition process
The order of execution of the child process will change, indicating that the three sub-processes in parallel.
The arguments:
from multiprocessing Import Process Import OS DEF func1 (args): # args reception parameters Print ( ' . 1 sub-process ' , os.getpid ()) # child process: the process acquires the current process number Print ( ' parent child process: ' , os.getppid ()) # get the process of the parent process the above mentioned id Print (args) # print parameters passed in IF __name__ == ' __main__ ' : p1 = process (target = func1, args = ( ' parameter ' ,)) #Passed as a tuple of parameters, if only to pass a parameter must be a comma p1.start () Print ( ' parent: ' , os.getpid ()) # parent: Get the current process of process ID Print ( ' : parent process of the parent process ' , os.getppid ()) # parent process id acquisition process
result:
Open multiple child process method:
from multiprocessing Import Process DEF func1 (args): Print ( ' * ' * args) IF the __name__ == ' __main__ ' : # may be used to achieve open loop for a plurality of sub-processes for I in Range (l, 5 ): P1 = Process ( = func1 target, args = (I,)) p1.start ()
Result 1: Result 2:
Multi-process join () method:
from multiprocessing Import Process DEF func1 (args): Print ( ' sub-process. 1 ' ) Print (args) IF the __name__ == ' __main__ ' : P1 = Process (target = func1, args = ( ' parameter ' ,)) p1.start () Print ( ' hahahahha ' ) # perceive the end of a child process will become asynchronous synchronization: before you join asynchronous p1.join () # after the join is synchronized Print ( ' qqqqqqqqq ' )
result:
Practice: We want to achieve output of 10 words asynchronous, synchronous final output of finished.
from multiprocessing import Process def func1(args): print('*'*args) if __name__ == '__main__': for i in range(10): p1 = Process(target=func1,args=(i,)) p1.start() p1.join() print('执行完毕!')
But we found, "finished" words always follow printout behind the longest sentence, 10 but because of the above sentence is asynchronous, the longest sentence is not always the final output, so we this can not realistically our needs.
from multiprocessing import Process def func1(args): print('*'*args) if __name__ == '__main__': p_list = [] #创建一个空列表用来存放等会生产的进程对象 for i in range(10): p1 = Process(target=func1,args=(i,)) p_list.append(p1)#将每个进程对象依次存放进列表中 p1.start() [i.join() for i in p_list] # 运用列表推导式:循环列表,依次对列表中的进程对象执行join()方法。之前所有进程必须在这里执行完才能执行下面的代码。 print('执行完毕!')
结果1: 结果2: