zoukankan      html  css  js  c++  java
  • 父线程,没有等子线程执行完就退出

    # -*- coding: utf-8 -*-
    import threading
    from time import ctime,sleep
    
    
    def music(func):
        for i in range(3):
            print "I was listening to %s. %s" %(func,ctime())
            sleep(2)
    
    def move(func):
        for i in range(5):
            print "I was at the %s! %s" %(func,ctime())
            sleep(5)
    
    threads = []
    t1 = threading.Thread(target=music,args=(u'爱情买卖',))
    threads.append(t1)
    t2 = threading.Thread(target=move,args=(u'阿凡达',))
    threads.append(t2)
    
    if __name__ == '__main__':
        for t in threads:
            #t.setDaemon(True)
            t.start()
        t.join()
        print "all over %s" %ctime()
    	
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.py
    I was listening to 爱情买卖. Sat Oct 14 13:15:54 2017
    I was at the 阿凡达! Sat Oct 14 13:15:54 2017
    I was listening to 爱情买卖. Sat Oct 14 13:15:56 2017
    I was listening to 爱情买卖. Sat Oct 14 13:15:58 2017
    I was at the 阿凡达! Sat Oct 14 13:15:59 2017
    I was at the 阿凡达! Sat Oct 14 13:16:04 2017
    I was at the 阿凡达! Sat Oct 14 13:16:09 2017
    I was at the 阿凡达! Sat Oct 14 13:16:14 2017
    all over Sat Oct 14 13:16:19 2017
    
    Process finished with exit code 0
    
    	
    此时是并行执行,执行的时间是25S
    
    # -*- coding: utf-8 -*-
    import threading
    from time import ctime,sleep
    
    
    def music(func):
        for i in range(3):
            print "I was listening to %s. %s" %(func,ctime())
            sleep(2)
    
    def move(func):
        for i in range(5):
            print "I was at the %s! %s" %(func,ctime())
            sleep(5)
    
    threads = []
    t1 = threading.Thread(target=music,args=(u'爱情买卖',))
    threads.append(t1)
    t2 = threading.Thread(target=move,args=(u'阿凡达',))
    threads.append(t2)
    
    if __name__ == '__main__':
        for t in threads:
            #t.setDaemon(True)
            t.start()
            t.join()
        print "all over %s" %ctime()
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.py
    I was listening to 爱情买卖. Sat Oct 14 13:17:10 2017
    I was listening to 爱情买卖. Sat Oct 14 13:17:12 2017
    I was listening to 爱情买卖. Sat Oct 14 13:17:14 2017
    I was at the 阿凡达! Sat Oct 14 13:17:16 2017
    I was at the 阿凡达! Sat Oct 14 13:17:21 2017
    I was at the 阿凡达! Sat Oct 14 13:17:26 2017
    I was at the 阿凡达! Sat Oct 14 13:17:31 2017
    I was at the 阿凡达! Sat Oct 14 13:17:36 2017
    all over Sat Oct 14 13:17:41 2017
    
    Process finished with exit code 0
    
    
    
    此时是串行,花了31S
    
    
    
    
    # -*- coding: utf-8 -*-
    import threading
    from time import ctime,sleep
    
    
    def music(func):
        for i in range(3):
            print "I was listening to %s. %s" %(func,ctime())
            sleep(2)
    
    def move(func):
        for i in range(5):
            print "I was at the %s! %s" %(func,ctime())
            sleep(5)
    
    threads = []
    t1 = threading.Thread(target=music,args=(u'爱情买卖',))
    threads.append(t1)
    t2 = threading.Thread(target=move,args=(u'阿凡达',))
    threads.append(t2)
    
    if __name__ == '__main__':
        for t in threads:
            t.setDaemon(True)
            t.start()
            #t.join()
        print "all over %s" %ctime()
    	
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.py
    I was listening to 爱情买卖. Sat Oct 14 13:21:49 2017
    I was at the 阿凡达! Sat Oct 14 13:21:49 2017
     all over Sat Oct 14 13:21:49 2017
    
    Process finished with exit code 0
    
    
    此时父线程,没有等子线程执行完就退出了

  • 相关阅读:
    缺少环境变量导致Xilinx Platform Studio无法打开之解决方法
    PS利用EMIO控制LED灯
    利用zedboard添加自定义IP核完成简易计算器
    读后感 关于 《不要一辈子靠技术混饭吃》(挪窝第一篇)
    挪窝了,再谈blog
    windows server 2008 无法打开角色,角色错误,刷新服务器时出现意外错误,HRESULT:0x80070422
    Linux 基础正则表达式和扩展正则表达式
    Linux 基础正则表达式
    Linux 通配符与特殊符号
    Linux 基础命令
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349515.html
Copyright © 2011-2022 走看看