多线程

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#线程和进程
#http://www.cnblogs.com/fnng/p/3670789.html



#多线程:
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):
        for i in range(2):
            print 'i was 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:
        #将线程声明为守护线程,
        #必须在start()方法调用之前设置,如果不设置为守护线程,程序会被无限挂起。
        t.setDaemon(True)
        #启动线程
        t.start()
    #用于等待线程终止,
        #作用:在子线程完成运行之前,这个子线程的父线程将一直阻塞。
    t.join()
    print "all over %s" %ctime()


        
#说明:
#创建数组threads,创建线程t1,使用threading.Thread()方法;
#在这个方法中调用music方法,target=music,args方法对music进行传参。
#子线程启动后,父线程也继续执行下去,当父线程执行完毕最后一条语句时,没有等待子线程,直接就退出了。同时子线程也一样结束。
#子线程music、move和主线程 print "all over %s" %ctime()都是同一时间启动,但由于主线程执行完结束,所以导致子线程也终止。
原文地址:https://www.cnblogs.com/dengyg200891/p/4940547.html