#join方法[并行]:主进程等待子进程运行完成 from multiprocessing import Process import time def work(name): print("target %s is running" %name) time.sleep(2) print("target %s is done" %name) if __name__=="__main__": p1=Process(target=work,args=("egon",)) p2=Process(target=work,args=("alex",)) # p1.start()#向操作系统发送请求,开启进程 # p2.start() # p1.join()#主进程等待p1结束 # p2.join() p_list=[p1,p2] for p in p_list: p.start() for p in p_list: p.join() print("主进程") #等待时间为程序运行时间最长的那个程序 ‘‘‘ target egon is running target alex is running target alex is done target egon is done 主进程 ‘‘‘
#join方法[串行]:主进程等待子进程运行完成 from multiprocessing import Process import time #并发,在同一个终端打印 def work(name): print("target %s is running" %name) time.sleep(2) print("target %s is done" %name) if __name__=="__main__": p1=Process(target=work,args=("egon",)) p2=Process(target=work,args=("alex",)) # p1.start()#向操作系统发送请求,开启进程 # p2.start() # p1.join()#主进程等待p1结束 # p2.join() p_list=[p1,p2] for p in p_list: p.start() p.join() print("主进程") #等待时间为程序运行时间最长的那个程序 ‘‘‘ target egon is running target egon is done target alex is running target alex is done 主进程 ‘‘‘
#join方法[并行]:主线程等待子线程运行完成 from threading import Thread import time def work(name): print("target %s is running" %name) time.sleep(2) print("target %s is done" %name) if __name__=="__main__": t1=Thread(target=work,args=("egon",)) t2=Thread(target=work,args=("alex",)) t_list=[t1,t2] for t in t_list: t.start() for t in t_list: t.join() print("主") ‘‘‘ target egon is running target alex is running target egon is done target alex is done 主 ‘‘‘
#join方法[串行]:主线程等待子线程运行完成 from threading import Thread import time def work(name): print("target %s is running" %name) time.sleep(2) print("target %s is done" %name) if __name__=="__main__": t1=Thread(target=work,args=("egon",)) t2=Thread(target=work,args=("alex",)) t_list=[t1,t2] for t in t_list: t.start() t.join() print("主") ‘‘‘ target egon is running target egon is done target alex is running target alex is done 主 ‘‘‘
原文:https://www.cnblogs.com/hapyygril/p/12589112.html