理解Python multiprocessing.Process start和join方法

齐博厚
2023-12-01

以下代码来自:https://www.cnblogs.com/lipijin/p/3709903.html,这里只是将其中print转为python3方式,通过依次看这几段代码以及运行结果应该能够理解start和join方法,文字描述反而可能会造成误解。

In [1]: #encoding:utf-8 
   ...: from multiprocessing import Process 
   ...: import os, time, random 
   ...:  
   ...: #线程启动后实际执行的代码块 
   ...: def r1(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...:  
   ...: def r2(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...:      
   ...: if __name__ == "__main__": 
   ...:     print("main process run...") 
   ...:     p1 = Process(target=r1, args=('process_name1', ))       #target:指定进程执行的函数,args:该函数的参数,需要使用tuple 
   ...:     p2 = Process(target=r2, args=('process_name2', )) 
   ...:          
   ...:     p1.start()    #通过调用start方法启动进程,跟线程差不多。 
   ...:     p2.start()    #但run方法在哪呢?待会说。。。 
   ...:     p1.join()     #join方法也很有意思,寻思了一下午,终于理解了。待会演示。 
   ...:     p2.join() 
   ...:     print("main process runned all lines...") 
   ...:                                                                                                                                                                                                            
main process run...
process_name1 7713
process_name2 7714
process_name1 7713
process_name2 7714
process_name2 7714
process_name1 7713
process_name1 7713
process_name2 7714
process_name1 7713
process_name2 7714
main process runned all lines...
In [1]: #encoding:utf-8 
   ...: from multiprocessing import Process 
   ...: import os, time, random 
   ...:  
   ...: def r(): 
   ...:     print('run method') 
   ...:      
   ...: if __name__ == "__main__": 
   ...:     print("main process run...") 
   ...:     #没有指定Process的targt 
   ...:     p1 = Process() 
   ...:     p2 = Process() 
   ...:     #如果在创建Process时不指定target,那么执行时没有任何效果。因为默认的run方法是判断如果不指定target,那就什么都不做 
   ...:     #所以这里手动改变了run方法 
   ...:     p1.run = r 
   ...:     p2.run = r 
   ...:          
   ...:     p1.start() 
   ...:     p2.start() 
   ...:     p1.join() 
   ...:     p2.join() 
   ...:     print("main process runned all lines...") 
   ...:                                                                                                                                                                                                            
main process run...
run method
run method
main process runned all lines...
In [1]: #encoding:utf-8 
   ...: from multiprocessing import Process 
   ...: import os, time, random 
   ...:  
   ...: def r1(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...: def r2(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...:  
   ...: if __name__ == "__main__": 
   ...:     print("main process run...") 
   ...:     p1 = Process(target=r1, args=('process_name1', ))  
   ...:     p2 = Process(target=r2, args=('process_name2', ))  
   ...:  
   ...:     p1.start() 
   ...:     p2.start() 
   ...:     #p1.join() 
   ...:     #p2.join()     
   ...:     print("main process runned all lines...") 
   ...:                                                                                                                                                                                                            
main process run...
process_name1 7881
main process runned all lines...
process_name2 7882
process_name2 7882                                                                                                                                                                                         
process_name2 7882
process_name2 7882
process_name2 7882
process_name1 7881
process_name1 7881
process_name1 7881
process_name1 7881
In [1]: #encoding:utf-8 
   ...: from multiprocessing import Process 
   ...: import os, time, random 
   ...:  
   ...: def r1(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...: def r2(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()*2) 
   ...:  
   ...: if __name__ == "__main__": 
   ...:     print("main process run...") 
   ...:     p1 = Process(target=r1, args=('process_name1', ))  
   ...:     p2 = Process(target=r2, args=('process_name2', ))  
   ...:  
   ...:     p1.start() 
   ...:     p2.start() 
   ...:     p1.join() 
   ...:     #p2.join()     
   ...:     print("main process runned all lines...") 
   ...:                                                                                                                                                                                                            
main process run...
process_name1 7938
process_name2 7939
process_name1 7938
process_name1 7938
process_name1 7938
process_name2 7939
process_name1 7938
process_name2 7939
main process runned all lines...

process_name2 7939                                                                                                                                                                                         
process_name2 7939
In [1]: #encoding:utf-8 
   ...: from multiprocessing import Process 
   ...: import os, time, random 
   ...:  
   ...: def r1(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()) 
   ...: def r2(process_name): 
   ...:     for i in range(5): 
   ...:         print(process_name, os.getpid())     #打印出当前进程的id 
   ...:         time.sleep(random.random()*2) 
   ...:  
   ...: if __name__ == "__main__": 
   ...:     print("main process run...") 
   ...:     p1 = Process(target=r1, args=('process_name1', ))  
   ...:     p2 = Process(target=r2, args=('process_name2', ))  
   ...:  
   ...:     p1.start() 
   ...:     p1.join() 
   ...:     p2.start() 
   ...:     #p2.join()     
   ...:     print("main process runned all lines...") 
   ...:                                                                                                                                                                                                            
main process run...
process_name1 7993
process_name1 7993
process_name1 7993
process_name1 7993
process_name1 7993
main process runned all lines...

process_name2 7994
process_name2 7994                                                                                                                                                                                        
process_name2 7994
process_name2 7994
process_name2 7994
 类似资料: