Python多線程中join函數與setDaemon函數使用說明

      在Python多線程編程的時候,常常須要用到join函數和setDaemon函數。以前對這兩個函數一直理解不是很到位。今天查閱了不少資料,對兩個函數的認識更加的深刻一些了。python

      join([timeout])能夠參考Python文檔說明。大概意思就是調用join函數會使得主調線程阻塞,直到被調用線程運行結束或超時。參數timeout是一個數值類型,用來表示超時時間,若是未提供該參數,那麼主調線程將一直阻塞直到被調用線程結束。編程

      借鑑一下別人的代碼說明狀況:多線程

#!/usr/bin/env python
import threading
import time
class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
    def run(self):
        apply(self.func, self.args)
def print_func(num):
    while True:
        print "I am thread%d" % num
        time.sleep(1)
threads = []
t1 = MyThread(print_func, (1, ), print_func.__name__)
threads.append(t1)
t2 = MyThread(print_func, (2, ), print_func.__name__)
threads.append(t2)
for t in threads:
    t.start()
    t.join()
print "ok\n"

      程序的運行輸出以下:app

2

      查看程序輸出發現只有第一個子線程在調用,第二個子線程以及父線程都沒有繼續走下去。這是由於join函數一直在等待子線程結束,可是循環使得子線程一直沒有結束,這樣後續子線程和主線程都阻塞了。使得只有第一個子線程在循環執行。函數

      改一下最後面的代碼爲:spa

for t in threads:
    t.start()
for t in threads:
    t.join()

      運行結果以下所示:.net

3

      能夠看到線程一和線程二在交替執行。兩個子線程完成以前,父線程的print "ok\n"都不會執行。線程

      修改一下使得兩個子線程運行時間不同,會是怎樣的結果呢?blog

#!/usr/bin/env python
import threading
import time
class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
    def run(self):
        apply(self.func, self.args)
def print_func1(num):
    while True:
        print "I am thread%d" % num
        time.sleep(1)
def print_func2(num):
    while True:
        print "I am thread%d" % num
        time.sleep(2)
threads = []
t1 = MyThread(print_func1, (1, ), print_func1.__name__)
threads.append(t1)
t2 = MyThread(print_func2, (2, ), print_func2.__name__)
threads.append(t2)
for t in threads:
    t.start()
for t in threads:
    t.join()
print "ok\n"

      運行結果以下圖所示:文檔

4 

      能夠看到一個子線程的完成不會影響另一個子線程,父線程仍然一直被阻塞,須要等待兩個子線程完成以後纔會打印結果。

      setDaemon()能夠參考Python文檔說明。大概意思就是能夠設置setDaemon的參數爲True來表示將該線程指定爲守護線程,若是參數爲False就不指定線程爲守護線程。設置setDaemon的參數爲True以後。主線程和子線程會同時運行,主線程結束運行後,不管子線程運行與否,都會和主線程一塊兒結束。

      借鑑一下別人的代碼說明狀況:

#!/usr/bin/env python
import threading
import time
class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self)
        self.name=name
        self.func=func
        self.args=args
    def run(self):
        apply(self.func, self.args)
def print_func(num):
    while True:
        print "I am thread%d" % num
        time.sleep(1)
threads = []
t1 = MyThread(print_func, (1, ), print_func.__name__)
threads.append(t1)
t2 = MyThread(print_func, (2, ), print_func.__name__)
threads.append(t2)
for t in threads:
    t.setDaemon(True)
    t.start()
print "ok\n"

      運行結果以下圖所示:

1

      查看程序輸出後能夠發現print_func函數中的循環沒有繼續執行下去就退出了,因爲setDaemon(True)把子線程設置爲守護線程,子線程啓動後,父線程也繼續執行下去,當父線程執行完最後一條語句print "ok\n"後,沒有等待子線程是否完成,直接就退出了,同時子線程也一同結束。

相關文章
相關標籤/搜索