標籤:join append 任務 存在 守護線程 inf 退出 參數 逾時
前言
今天小編YOYO請xiaoming和xiaowang吃火鍋,吃完火鍋的時候會有以下三種情境:
情境一:小編(主)先吃完了,xiaoming(客)和xiaowang(客)還沒吃完,這種情境會導致結賬的人先走了,剩下兩個小夥伴傻眼了。。。
情境二:小編(主)先吃完了,xiaoming和xiaowang還沒吃飽,一起結賬走人。
情境三:小編(主)先等xiaoming和xiaowang吃飽了,小編最後結賬一起走人。
主線程與子線程
情境一:主線程已經結束了,子線程還在跑
1.我們把thread1.start()和thread2.start()稱為兩個子線程,寫在外面的代碼就是主線程了。
# coding=utf-8import threadingimport timedef chiHuoGuo(people): print("%s 吃火鍋的小夥伴-羊肉:%s" % (time.ctime(),people)) time.sleep(1) print("%s 吃火鍋的小夥伴-魚丸:%s" % (time.ctime(),people))class myThread (threading.Thread): # 繼承父類threading.Thread def __init__(self, people, name): ‘‘‘重寫threading.Thread初始化內容‘‘‘ threading.Thread.__init__(self) self.threadName = name self.people = people def run(self): # 把要執行的代碼寫到run函數裡面 線程在建立後會直接運行run函數 ‘‘‘重寫run方法‘‘‘ print("開始線程: " + self.threadName) chiHuoGuo(self.people) # 執行任務 print("qq交流群:226296743") print("結束線程: " + self.name)print("yoyo請小夥伴開始吃火鍋:!!!")# 建立新線程thread1 = myThread("xiaoming", "Thread-1")thread2 = myThread("xiaowang", "Thread-2")# 開啟線程thread1.start()thread2.start()time.sleep(0.1)print("退出主線程:吃火鍋結束,結賬走人")
2.運行結果:
守護線程setDaemon()
情境二:主線程結束了,子線程必須也跟著結束
1.主線程中,建立了子線程thread1和thread2,並且在主線程中調用了thread.setDaemon(),這個的意思是,把主線程設定為守護線程,這時候,要是主線程執行結束了,就不管子線程是否完成,一併和主線程退出.
(敲黑板:必須在start()方法調用之前設定,如果不設定為守護線程,程式會被無限掛起。)
2.線程有一個布爾屬性叫做daemon。表示線程是否是守護線程,預設取否。當程式中的線程全部是守護線程時,程式才會退出。只要還存在一個非守護線程,程式就不會退出。
主線程是非守護線程。
3.setDaemon(True)此方法裡面參數設定為True才會生效
# coding=utf-8import threadingimport timedef chiHuoGuo(people): print("%s 吃火鍋的小夥伴-羊肉:%s" % (time.ctime(),people)) time.sleep(1) print("%s 吃火鍋的小夥伴-魚丸:%s" % (time.ctime(),people))class myThread (threading.Thread): # 繼承父類threading.Thread def __init__(self, people, name): ‘‘‘重寫threading.Thread初始化內容‘‘‘ threading.Thread.__init__(self) self.threadName = name self.people = people def run(self): # 把要執行的代碼寫到run函數裡面 線程在建立後會直接運行run函數 ‘‘‘重寫run方法‘‘‘ print("開始線程: " + self.threadName) chiHuoGuo(self.people) # 執行任務 print("qq交流群:226296743") print("結束線程: " + self.name)print("yoyo請小夥伴開始吃火鍋:!!!")# 建立新線程thread1 = myThread("xiaoming", "Thread-1")thread2 = myThread("xiaowang", "Thread-2")# 守護線程setDaemon(True)thread1.setDaemon(True) # 必須在start之前thread2.setDaemon(True)# 開啟線程thread1.start()thread2.start()time.sleep(0.1)print("退出主線程:吃火鍋結束,結賬走人")
4.運行結果:
阻塞主線程join(timeout)
1.如果想讓主線程等待子線程結束後再啟動並執行話,就需要用到join(),此方法是在start之後(與setDaemon相反)
2.join(timeout)此方法有個timeout參數,是線程逾時時間設定。
# coding=utf-8import threadingimport timedef chiHuoGuo(people): print("%s 吃火鍋的小夥伴-羊肉:%s" % (time.ctime(),people)) time.sleep(1) print("%s 吃火鍋的小夥伴-魚丸:%s" % (time.ctime(),people))class myThread (threading.Thread): # 繼承父類threading.Thread def __init__(self, people, name): ‘‘‘重寫threading.Thread初始化內容‘‘‘ threading.Thread.__init__(self) self.threadName = name self.people = people def run(self): # 把要執行的代碼寫到run函數裡面 線程在建立後會直接運行run函數 ‘‘‘重寫run方法‘‘‘ print("開始線程: " + self.threadName) chiHuoGuo(self.people) # 執行任務 print("qq交流群:226296743") print("結束線程: " + self.name)print("yoyo請小夥伴開始吃火鍋:!!!")# 建立新線程thread1 = myThread("xiaoming", "Thread-1")thread2 = myThread("xiaowang", "Thread-2")# 開啟線程thread1.start()thread2.start()# 阻塞主線程,等子線程結束thread1.join()thread2.join()time.sleep(0.1)print("退出主線程:吃火鍋結束,結賬走人")
運行結果:
參考代碼:
# coding=utf-8import threadingimport timedef chiHuoGuo(people): print("%s 吃火鍋的小夥伴-羊肉:%s" % (time.ctime(),people)) time.sleep(1) print("%s 吃火鍋的小夥伴-魚丸:%s" % (time.ctime(),people))class myThread (threading.Thread): # 繼承父類threading.Thread def __init__(self, people, name): ‘‘‘重寫threading.Thread初始化內容‘‘‘ threading.Thread.__init__(self) self.threadName = name self.people = people def run(self): # 把要執行的代碼寫到run函數裡面 線程在建立後會直接運行run函數 ‘‘‘重寫run方法‘‘‘ print("開始線程: " + self.threadName) chiHuoGuo(self.people) # 執行任務 print("qq交流群:226296743") print("結束線程: " + self.name)print("yoyo請小夥伴開始吃火鍋:!!!")# 設定線程組threads = []# 建立新線程thread1 = myThread("xiaoming", "Thread-1")thread2 = myThread("xiaowang", "Thread-2")# 添加到線程組threads.append(thread1)threads.append(thread2)# 開啟線程for thread in threads: thread.start()# 阻塞主線程,等子線程結束for thread in threads: thread.join()time.sleep(0.1)print("退出主線程:吃火鍋結束,結賬走人")
python筆記9-多線程Threading之阻塞(join)和守護線程(setDaemon)