zoukankan      html  css  js  c++  java
  • Python 多线程编程之join()

    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(5)
    
    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/thread/p1.py
    I was listening to 爱情买卖. Thu Aug 24 16:49:52 2017
    I was at the 阿凡达! Thu Aug 24 16:49:52 2017
    I was listening to 爱情买卖. Thu Aug 24 16:49:57 2017
    I was at the 阿凡达! Thu Aug 24 16:49:57 2017
    I was at the 阿凡达! Thu Aug 24 16:50:02 2017I was listening to 爱情买卖. Thu Aug 24 16:50:02 2017
    
    I was at the 阿凡达! Thu Aug 24 16:50:07 2017
    I was at the 阿凡达! Thu Aug 24 16:50:12 2017
    all over Thu Aug 24 16:50:17 2017
    
    Process finished with exit code 0
    
    
    
    #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(10)
    
    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(5)
        print "all over %s" %ctime()
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/thread/p3.py
    I was listening to 爱情买卖. Thu Aug 24 15:45:47 2017
    I was at the 阿凡达! Thu Aug 24 15:45:47 2017
    I was at the 阿凡达! Thu Aug 24 15:45:52 2017
    all over Thu Aug 24 15:45:52 2017
    I was listening to 爱情买卖. Thu Aug 24 15:45:57 2017
    I was at the 阿凡达! Thu Aug 24 15:45:57 2017
    I was at the 阿凡达! Thu Aug 24 15:46:02 2017
    I was listening to 爱情买卖. Thu Aug 24 15:46:07 2017
    I was at the 阿凡达! Thu Aug 24 15:46:07 2017
    
    Process finished with exit code 0
    
    
     join([timeout])
    
    等待知道线程终止,这个堵塞调用线程直到线程join()方法是被调用终止。
    
    正常或者通过一个未知异常--或者直到可选的超时发生
    
    
    当超时超时是存在且不为空,它应该是一个数值 单位是秒。
    
    join()总是返回None,你必须调用isAlive()在join()来决定是否一个timeout 发生


    
                                        
    
  • 相关阅读:
    HTML 样式设计
    Spring Spring mvc MyBatis 中使用数据库查询别名进行映射
    常见数据库优化方案(六)
    别人面试案例(一)
    常用数据库优化方案(四)
    AS 实机测试 ADB.exe 提示
    指尖上的正则表达式–入门篇
    程序员对内存的理解
    字符串匹配的KMP算法
    字符串匹配的Boyer-Moore算法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349568.html
Copyright © 2011-2022 走看看