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
    
    
    此时父线程,没有等子线程执行完就退出了

  • 相关阅读:
    输入一个nxn矩阵各元素的值,球出两条对角线元素之和
    打印杨辉三角
    编写一个函数,实现两个字符串的连接功能
    字符串置换。将字符串s中的出现的字符s1用字符s2置换
    有一行文字,要求删去其中某个字符
    自定义函数delstr()的功能是删去字符串s1中所有的"*"
    用微软的kestrel在Linux上利用Apache架设Asp.Net Core环境
    2012年8月14日 星期二 equals()方法 (冲突备份)
    jquery 操作DOM 案例
    FileUpload 控件上传图片和文件
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349515.html
Copyright © 2011-2022 走看看