python threading中處理主程序和子執行緒的關係
阿新 • • 發佈:2019-02-02
之前用python的多執行緒,總是處理不好程序和執行緒之間的關係。後來發現了join和setDaemon函式,才終於弄明白。下面總結一下。
1.使用join函式後,主程序會在呼叫join的地方等待子執行緒結束,然後才接著往下執行。
join使用例項如下:
執行結果如下:import time import random import threading class worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): t = random.randint(1,10) time.sleep(t) print "This is " + self.getName() + ";I sleep %d second."%(t) tsk = [] for i in xrange(0,5): time.sleep(0.1) thread = worker() thread.start() tsk.append(thread) for tt in tsk: tt.join() print "This is the end of main thread."
# python testjoin.py
This is Thread-3;I sleep 2 second.
This is Thread-1;I sleep 4 second.
This is Thread-2;I sleep 7 second.
This is Thread-4;I sleep 7 second.
This is Thread-5;I sleep 7 second.
This is the end of main thread.
這裡建立了5個子執行緒,每個執行緒隨機等待1-10秒後列印退出;主執行緒分別等待5個子執行緒結束。最後結果是先顯示各個子執行緒,再顯示主程序的結果。
2. 如果使用的setDaemon函式,則與join相反,主程序結束的時候不會等待子執行緒。
setDaemon函式使用例項:
這裡設定主程序為守護程序,當主程序結束的時候,子執行緒被中止import time import random import threading class worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): t = random.randint(1,10) time.sleep(t) print "This is " + self.getName() + ";I sleep %d second."%(t) tsk = [] for i in xrange(0,5): time.sleep(0.1) thread = worker() thread.setDaemon(True) thread.start() tsk.append(thread) print "This is the end of main thread."
執行結果如下:
#python testsetDaemon.py
This is the end of main thread.
3、如果沒有使用join和setDaemon函式,則主程序在建立子執行緒後,直接執行後面的程式碼,主程式一直掛起,直到子執行緒結束才能結束。
import time
import random
import threading
class worker(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
t = random.randint(1,10)
time.sleep(t)
print "This is " + self.getName() + ";I sleep %d second."%(t)
tsk = []
for i in xrange(0,5):
time.sleep(0.1)
thread = worker()
thread.start()
tsk.append(thread)
print "This is the end of main thread."
執行結果如下:
# python testthread.py
This is the end of main thread.
This is Thread-4;I sleep 1 second.
This is Thread-3;I sleep 7 second.
This is Thread-5;I sleep 7 second.
This is Thread-1;I sleep 10 second.
This is Thread-2;I sleep 10 second.