python筆記9-多線程Threading之阻塞(join)和守護線程(setDaemon)


前言

今天小王請xiaoming和xiaowang吃火鍋,吃完火鍋的時候會有以下三種場景:

  • 場景一:小王(主)先吃完了,海海(客)和老王(客)還沒吃完,這種場景會導致結賬的人先走了,剩下兩個小伙伴傻眼了。。。

  • 場景二:小王(主)先吃完了,海海和老王還沒吃飽,一起結賬走人。

  • 場景三:小王(主)先等海海和老王吃飽了,小編最后結賬一起走人。

主線程與子線程

場景一:主線程已經結束了,子線程還在跑

1.我們把thread1.start()和thread2.start()稱為兩個子線程,寫在外面的代碼就是主線程了。



# -*- coding:utf-8 -*-
import time
import threading

def chiHuoGuo(people):
print("%s 吃火鍋的小伙伴-羊肉:%s" % (time.ctime(),people))
time.sleep(1)
print("%s 吃火鍋的小伙伴-魚丸:%s" % (time.ctime(),people))

class myThread(threading.Thread):
def __init__(self, people, name):
'''重寫threading.Thread初始化內容'''
threading.Thread.__init__(self)
self.threadNmae = name
self.people = people

def run(self):
print("開始線程:" + self.threadNmae)

chiHuoGuo(self.people)
print("結束線程:" + self.threadNmae)

if __name__ == "__main__":
# 啟動線程
thread1 = myThread("xiaoming", "Thread-1")
thread2 = myThread("xiaowang", "Thread-2")

thread1.start()
thread2.start()

time.sleep(0.1)
print("退出主線程:吃火鍋,結賬走人")

 

守護線程setDaemon()

場景二:主線程結束了,子線程必須也跟着結束

1.主線程中,創建了子線程 線程A和線程B,並且在主線程中調用了thread.setDaemon(),這個的意思是,把主線程設置為守護線程,這時候,要是主線程執行結束了,就不管子線程是否完成,一並和主線程退出.
(敲黑板:必須在start()方法調用之前設置,如果不設置為守護線程,程序會被無限掛起。)

2.線程有一個布爾屬性叫做daemon。表示線程是否是守護線程,默認取否。當程序中的線程全部是守護線程時,程序才會退出。只要還存在一個非守護線程,程序就不會退出。
主線程是非守護線程。

3.setDaemon(True)此方法里面參數設置為True才會生效

# coding=utf-8
import threading
import time

def 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("結束線程: " + 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-8 import threading import time def 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("結束線程: " + 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("退出主線程:吃火鍋結束,結賬走人")

運行結果:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM