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


    
                                        
    
  • 相关阅读:
    面试官:Redis 有哪些拓展方案?
    面试官:为什么要合并 HTTP 请求?
    Java 调用第三方接口,实战来了!
    Java 如何模拟真正的并发请求?
    如何搭建一台永久运行的个人服务器?试试这个黑科技!
    vs2005 sp1 出来啦!!
    2007年第一帖
    xp pro sp2支持多个用户同时终端连接
    msn中实现 "添加一个活动或游戏邀请"
    softether
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349568.html
Copyright © 2011-2022 走看看