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

  • 相关阅读:
    serial number
    python getopt
    python readline,seek
    linux scp
    jenkinsapi
    windows kill process
    python time
    python configparse
    解决某些.net不方便解决的问题,解决方法就是 DHTML
    (转)windows XP 系统服务“关闭”详细列表,释放N多内存,128也够用了!
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349515.html
Copyright © 2011-2022 走看看