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 发生


    
                                        
    
  • 相关阅读:
    管理技巧,绩效考核自评怎么写
    网站运营文章LIST
    搜索引擎算法研究专题六:HITS算法
    搜索引擎算法研究专题五:TF-IDF详解
    搜索引擎算法研究专题七:Hilltop算法
    搜索引擎算法研究专题四:随机冲浪模型介绍
    搜索引擎算法研究专题三:聚集索引与非聚集索引介绍
    利用Lucene.net搜索引擎进行多条件搜索的做法
    五大主流数字币钱包:imToken数字货币钱包,Bitcoin core钱包,BTS网页版钱包,AToken轻钱包,Blockchain
    Java 9 逆天的十大新特性
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349568.html
Copyright © 2011-2022 走看看