Python的多线程

来源:互联网 发布:免费建站 知乎 编辑:程序博客网 时间:2024/06/11 00:20

#coding=utf-8


#包含threading 模块

import threading
from time import ctime,sleep


def music(func):
    for i in range(2):
        print "I was listening to %s. %s" %(func,ctime())
        sleep(1)

def move(func):
    for i in range(2):
        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:

       #setDaemon(True)将线程声明为守护线程,必须在start() 方法调用之前设置,如果不设置为守护线程程序会被无限挂起。

        t.setDaemon(True)

        t.start()

    #join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

    t.join()


    print "all over %s" %ctime()
原创粉丝点击