线程threeding: + join
练习:
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ‘‘‘ 4 Administrator 5 2018/8/10 6 ‘‘‘ 7 8 import threading 9 from time import time,sleep,asctime,ctime 10 11 def music(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 def move(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 for t in threads: 31 t.start() 32 t.join() 33 34 35 print("all is end.")
1 import threading 2 from time import ctime,sleep 3 import time 4 5 def music(func): 6 for i in range(2): 7 print ("Begin listening to %s. %s" %(func,ctime())) 8 sleep(4) 9 print("end listening %s"%ctime()) 10 11 def move(func): 12 for i in range(2): 13 print ("Begin watching at the %s! %s" %(func,ctime())) 14 sleep(5) 15 print(‘end watching %s‘%ctime()) 16 17 threads = [] 18 t1 = threading.Thread(target=music,args=(‘七里香‘,)) 19 threads.append(t1) 20 t2 = threading.Thread(target=move,args=(‘阿甘正传‘,)) 21 threads.append(t2) 22 23 if __name__ == ‘__main__‘: 24 25 for t in threads: 26 # t.setDaemon(True) 27 t.start() 28 # t.join() 29 # t1.join() 30 t2.join()########考虑这三种join位置下的结果? 31 print ("all over %s" %ctime())
setDaemon(True):
将线程声明为守护线程,必须在start() 方法调用之前设置, 如果不设置为守护线程程序会被无限挂起。这个方法基本和join是相反的。当我们 在程序运行中,执行一个主线程,如果主线程又创建一个子线程,主线程和子线程 就分兵两路,分别运行,那么当主线程完成想退出时,会检验子线程是否完成。如 果子线程未完成,则主线程会等待子线程完成后再退出。但是有时候我们需要的是 只要主线程完成了,不管子线程是否完成,都要和主线程一起退出,这时就可以 用setDaemon方法啦
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ‘‘‘ 4 Administrator 5 2018/8/10 6 ‘‘‘ 7 8 import threading 9 from time import time,sleep,asctime,ctime 10 11 def music(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 def move(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 for t in threads: 31 t.setDaemon(True)#守护线程 32 t.start() 33 # t.join() 34 print("all is end. %s"%ctime())
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ‘‘‘ 4 Administrator 5 2018/8/10 6 ‘‘‘ 7 8 import threading 9 from time import time,sleep,asctime,ctime 10 11 def music(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 def move(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 in threads: 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 2018 39 ......move0 .. 我不是药神 is doing Fri Aug 10 12:21:46 2018 40 all is end. Fri Aug 10 12:21:46 2018 41 .on mygod.....music 0.. 七里香 is end Fri Aug 10 12:21:48 2018 42 ......music 1 .. 七里香 is doing Fri Aug 10 12:21:48 2018 43 .on mygod..0...move .. 我不是药神 is end Fri Aug 10 12:21:49 2018 44 ......move1 .. 我不是药神 is doing Fri Aug 10 12:21:49 2018 45 .on mygod.....music 1.. 七里香 is end Fri Aug 10 12:21:50 2018 46 .on mygod..1...move .. 我不是药神 is end Fri Aug 10 12:21:52 2018 47 48 -----------------------t2.setDaemon(True)---------------------------------------------- 49 ......music 0 .. 七里香 is doing Fri Aug 10 12:20:30 2018 50 ......move0 .. 我不是药神 is doing Fri Aug 10 12:20:30 2018 51 all is end. Fri Aug 10 12:20:30 2018 52 .on mygod.....music 0.. 七里香 is end Fri Aug 10 12:20:32 2018 53 ......music 1 .. 七里香 is doing Fri Aug 10 12:20:32 2018 54 .on mygod..0...move .. 我不是药神 is end Fri Aug 10 12:20:33 2018 55 ......move1 .. 我不是药神 is doing Fri Aug 10 12:20:33 2018 56 .on mygod.....music 1.. 七里香 is end Fri Aug 10 12:20:34 2018 #### t1结束后, 程序就立即结束 57 58 """
守护进程
join():
在子线程完成运行之前,这个子线程的父线程将一直被阻塞。
其它方法
1 thread 模块提供的其他方法: 2 # threading.currentThread(): 返回当前的线程变量。 3 # threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。 4 # threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。 5 # 除了使用方法外,线程模块同样提供了Thread类来处理线程,Thread类提供了以下方法: 6 # run(): 用以表示线程活动的方法。 7 # start():启动线程活动。 8 # join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。 9 # isAlive(): 返回线程是否活动的。 10 # getName(): 返回线程名。 11 # setName(): 设置线程名。
原文地址:https://www.cnblogs.com/Mengchangxin/p/9454285.html
时间: 2024-10-02 23:01:35