首页 > 编程语言 > 详细

进程线程_join

时间:2020-03-28 20:57:40      阅读:57      评论:0      收藏:0      [点我收藏+]
#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
主
‘‘‘

 

进程线程_join

原文:https://www.cnblogs.com/hapyygril/p/12589112.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!