标签:pre lis 运行 结束 like main 结果 imp daemon
import threading
from time import ctime,sleep
import time
def music(name):
print(‘listening to {name}.{time}‘.format(name=name,time=ctime()))
sleep(3)
print(‘end {time}‘.format(time=ctime()))
def blog(title):
print(‘recording the {title}.{time}‘.format(title=title,time=ctime()))
sleep(5)
print(‘end {time}‘.format(time=ctime()))
threads =[]
t1 =threading.Thread(target=music,args=(‘like‘,))
t2 =threading.Thread(target=blog,args=(‘beijing‘,))
threads.append(t1)
threads.append(t2)
if __name__ == ‘__main__‘:
# t1.setDaemon(True) #注:一定要在start之前设置.它说明在主线程是否结束不考虑
# t1线程是否运行结束,故主线程是否结束于t2线程有关
t2.setDaemon(True) #运行的结果与t1.setDaemon(True)的结果不同的然因是两个
# 线程运行结束的时间不同
for i in threads: #运行t1和t2线程
i.start()
#t2.join()
print(‘all over %s‘ % ctime())
标签:pre lis 运行 结束 like main 结果 imp daemon
原文地址:http://www.cnblogs.com/IQ-Python/p/6825947.html