父线程,没有等子线程执行完就退出

来源:互联网 发布:淘宝售假降权多久恢复 编辑:程序博客网 时间:2024/06/01 08:27
# -*- coding: utf-8 -*-import threadingfrom time import ctime,sleepdef 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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.pyI was listening to 爱情买卖. Sat Oct 14 13:15:54 2017I was at the 阿凡达! Sat Oct 14 13:15:54 2017I was listening to 爱情买卖. Sat Oct 14 13:15:56 2017I was listening to 爱情买卖. Sat Oct 14 13:15:58 2017I was at the 阿凡达! Sat Oct 14 13:15:59 2017I was at the 阿凡达! Sat Oct 14 13:16:04 2017I was at the 阿凡达! Sat Oct 14 13:16:09 2017I was at the 阿凡达! Sat Oct 14 13:16:14 2017all over Sat Oct 14 13:16:19 2017Process finished with exit code 0此时是并行执行,执行的时间是25S# -*- coding: utf-8 -*-import threadingfrom time import ctime,sleepdef 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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.pyI was listening to 爱情买卖. Sat Oct 14 13:17:10 2017I was listening to 爱情买卖. Sat Oct 14 13:17:12 2017I was listening to 爱情买卖. Sat Oct 14 13:17:14 2017I was at the 阿凡达! Sat Oct 14 13:17:16 2017I was at the 阿凡达! Sat Oct 14 13:17:21 2017I was at the 阿凡达! Sat Oct 14 13:17:26 2017I was at the 阿凡达! Sat Oct 14 13:17:31 2017I was at the 阿凡达! Sat Oct 14 13:17:36 2017all over Sat Oct 14 13:17:41 2017Process finished with exit code 0此时是串行,花了31S# -*- coding: utf-8 -*-import threadingfrom time import ctime,sleepdef 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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/Django/a4.pyI was listening to 爱情买卖. Sat Oct 14 13:21:49 2017I was at the 阿凡达! Sat Oct 14 13:21:49 2017 all over Sat Oct 14 13:21:49 2017Process finished with exit code 0此时父线程,没有等子线程执行完就退出了