1 #!/usr/bin/env python3
2 #-*- coding:utf-8 -*-
3 ‘‘‘
4 Administrator5 2018/8/106 ‘‘‘
7
8 importthreading9 from time importtime,sleep,asctime,ctime10
11 defmusic(name):12 for i in range(2):13 print("......music %s .. %s is doing %s"%(i,name,ctime()))14 sleep(2)15 print(".on mygod.....music %s.. %s is end %s" %(i,name,ctime()))16 defmove(name):17 for i in range(2):18
19 print("......move%s .. %s is doing %s"%(i,name,ctime()))20 sleep(3)21 print(".on mygod..%s...move .. %s is end %s" %(i,name,ctime()) )22
23 threads=[]24 t1=threading.Thread(target=music,args=("七里香",))25 threads.append(t1)26 t2=threading.Thread(target=move,args=("我不是药神",))27 threads.append(t2)28
29 if __name__=="__main__":30 t2.setDaemon(True) #守护线程
31 for t inthreads:32 #t.setDaemon(True)#守护线程
33 t.start()34 #t.join()
35 print("all is end. %s"%ctime())36
37 """
38 ......music 0 .. 七里香 is doing Fri Aug 10 12:21:46 201839 ......move0 .. 我不是药神 is doing Fri Aug 10 12:21:46 201840 all is end. Fri Aug 10 12:21:46 201841 .on mygod.....music 0.. 七里香 is end Fri Aug 10 12:21:48 201842 ......music 1 .. 七里香 is doing Fri Aug 10 12:21:48 201843 .on mygod..0...move .. 我不是药神 is end Fri Aug 10 12:21:49 201844 ......move1 .. 我不是药神 is doing Fri Aug 10 12:21:49 201845 .on mygod.....music 1.. 七里香 is end Fri Aug 10 12:21:50 201846 .on mygod..1...move .. 我不是药神 is end Fri Aug 10 12:21:52 201847
48 -----------------------t2.setDaemon(True)----------------------------------------------49 ......music 0 .. 七里香 is doing Fri Aug 10 12:20:30 201850 ......move0 .. 我不是药神 is doing Fri Aug 10 12:20:30 201851 all is end. Fri Aug 10 12:20:30 201852 .on mygod.....music 0.. 七里香 is end Fri Aug 10 12:20:32 201853 ......music 1 .. 七里香 is doing Fri Aug 10 12:20:32 201854 .on mygod..0...move .. 我不是药神 is end Fri Aug 10 12:20:33 201855 ......move1 .. 我不是药神 is doing Fri Aug 10 12:20:33 201856 .on mygod.....music 1.. 七里香 is end Fri Aug 10 12:20:34 2018 #### t1结束后, 程序就立即结束57
58 """