一、什麼是 Threading
Threading用於提供線程相關的操作,線程是應用程序中工作的最小單元。python當前版本的多線程庫沒有實現優先級、線程組,線程也不能被停止、暫停、恢復、中斷。
1.1、線程池圖解
二、創建線程
導入模塊threading,通過threading.Thread()創建線程。其中target接收的是要執行的函數名字,args接收傳入函數的參數,以元組()的形式表示。
import threading
def foo(n):
print("foo%s"%n)
t1 = threading.Thread(target=foo,args=(1,)) #創建線程對象
三、啓動線程
通過線程對象t1.start()或t2.start()啓動線程。
t1 = threading.Thread(target=sayhi, args=(1,)) # 生成一個線程實例
t2 = threading.Thread(target=sayhi, args=(2,)) # 生成另一個線程實例
t1.start() # 啓動線程
t2.start() # 啓動另一個線程
實例1:
import threading
import time
def foo(n):
print("foo%s"%n)
time.sleep(1)
def bar(n):
print("bar%s"%n)
time.sleep(2)
t1 = threading.Thread(target=foo,args=(1,))
t2 = threading.Thread(target=bar,args=(2,))
t1.start()
t2.start()
print("...in the main...")
結果:
程序啓動後,主線程從上到下依次執行,t1、t2兩個子線程啓動後,與主線程並行,搶佔CPU資源。因此,前三行的輸出結果幾乎同時打印,沒有先後順序,此時,需要等t1和t2都結束後程序才結束。故等待2s後,程序結束。程序總共花了2s。
foo1
bar2
...in the main...
Process finished with exit code 0
實例2:
import threading
from time import ctime,sleep
import time
def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime())
def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime())
threads = []
t1 = threading.Thread(target=music,args=('周杰倫',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('梁朝偉',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
print ("all over %s" %ctime())
結果:
Begin listening to 周杰倫. Thu Sep 29 14:21:55 2016
Begin watching at the 梁朝偉! Thu Sep 29 14:21:55 2016
all over Thu Sep 29 14:21:55 2016
end listening Thu Sep 29 14:21:59 2016
Begin listening to 周杰倫. Thu Sep 29 14:21:59 2016
end watching Thu Sep 29 14:22:00 2016
Begin watching at the 梁朝偉! Thu Sep 29 14:22:00 2016
end listening Thu Sep 29 14:22:03 2016
end watching Thu Sep 29 14:22:05 2016
Process finished with exit code 0
四、join()
在子線程執行完成之前,這個子線程的父線程將一直被阻塞。就是說,當調用join()的子進程沒有結束之前,主進程不會往下執行。對其它子進程沒有影響。
實例1:
import threading
from time import ctime,sleep
import time
def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime())
def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime())
threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正傳',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
t.join()
print ("all over %s" %ctime())
結果解析:
t1線程啓動→Begin listening→4s後end listening + Begin listening →4s後t2線程啓動end listening t1結束 + Begin watching→5s後end listening + Begin watching→5s後end listening t2結束+ all over最後主進程結束。 就是醬紫,有點亂。。。
Begin listening to 七里香. Thu Sep 29 15:00:09 2016
end listening Thu Sep 29 15:00:13 2016
Begin listening to 七里香. Thu Sep 29 15:00:13 2016
end listening Thu Sep 29 15:00:17 2016
Begin watching at the 阿甘正傳! Thu Sep 29 15:00:17 2016
end watching Thu Sep 29 15:00:22 2016
Begin watching at the 阿甘正傳! Thu Sep 29 15:00:22 2016
end watching Thu Sep 29 15:00:27 2016
all over Thu Sep 29 15:00:27 2016
實例2:
import threading
from time import ctime,sleep
import time
def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime())
def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime())
threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正傳',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
t.join() #for循環的最後一次t的值,相當於t2
print ("all over %s" %ctime()
結果:
Begin listening to 七里香. Thu Sep 29 15:16:41 2016 #t1和t2線程啓動
Begin watching at the 阿甘正傳! Thu Sep 29 15:16:41 2016
end listening Thu Sep 29 15:16:45 2016
Begin listening to 七里香. Thu Sep 29 15:16:45 2016
end watching Thu Sep 29 15:16:46 2016
Begin watching at the 阿甘正傳! Thu Sep 29 15:16:46 2016
end listening Thu Sep 29 15:16:49 2016 #t1結束
end watching Thu Sep 29 15:16:51 2016 #t2結束,t2結束之前,主線程一直被阻塞。t2結束主線程繼續執行
all over Thu Sep 29 15:16:51 2016 #主線程結束
實例3:
import threading
from time import ctime,sleep
import time
def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime())
def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime())
threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正傳',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
t1.join() #當t1調用join()時
print ("all over %s" %ctime())
結果:
Begin listening to 七里香. Thu Sep 29 15:35:35 2016 #t1和t2啓動
Begin watching at the 阿甘正傳! Thu Sep 29 15:35:35 2016
end listening Thu Sep 29 15:35:39 2016
Begin listening to 七里香. Thu Sep 29 15:35:39 2016
end watching Thu Sep 29 15:35:40 2016
Begin watching at the 阿甘正傳! Thu Sep 29 15:35:40 2016
end listening Thu Sep 29 15:35:43 2016 #t1結束,主線程繼續往下執行
all over Thu Sep 29 15:35:43 2016 #主線程結束
end watching Thu Sep 29 15:35:45 2016 #t2結束
五、setDaemon(True)
將線程聲明爲守護線程,必須在start() 方法調用之前設置, 如果不設置爲守護線程程序會被無限掛起。這個方法基本和join是相反的。當我們 在程序運行中,執行一個主線程,如果主線程又創建一個子線程,主線程和子線程 就兵分兩路,分別運行,那麼當主線程完成想退出時,會檢驗子線程是否完成。如 果子線程未完成,則主線程會等待子線程完成後再退出。但是有時候我們需要的是 只要主線程完成了,不管子線程是否完成,都要和主線程一起退出,這時就可以用setDaemon方法。
實例:
import threading
from time import ctime,sleep
import time
def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(4)
print("end listening %s"%ctime())
def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(5)
print('end watching %s'%ctime())
threads = []
t1 = threading.Thread(target=music,args=('七里香',))
threads.append(t1)
t2 = threading.Thread(target=move,args=('阿甘正傳',))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.setDaemon(True)
t.start()
print ("all over %s" %ctime())
結果:
Begin listening to 七里香. Thu Sep 29 15:45:32 2016 #t1和t2啓動,分別打印一次後sleep,主進程繼續
Begin watching at the 阿甘正傳! Thu Sep 29 15:45:32 2016
all over Thu Sep 29 15:45:32 2016 #主進程結束,程序結束
六、current_thread()
獲取當前進程的名稱。
七、同步鎖
import time
import threading
def addNum():
global num #在每個線程中都獲取這個全局變量
# num-=1
temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp-1 #對此公共變量進行-1操作
num = 100 #設定一個共享變量
thread_list = []
for i in range(100):
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t)
for t in thread_list: #等待所有線程執行完畢
t.join()
print('final num:', num )
用num -= 1則最終結果沒問題,這是因爲完成這個操作太快了,在線程切換時間內。用中間變量temp進行賦值時出現問題,這是因爲100個線程,每一個都沒有執行完就就行了切換,因此最終得到的不是0。
多個線程同時操作同一個共享資源,所以導致衝突,這種情況就需要用同步鎖來解決。
import time
import threading
def addNum():
global num #在每個線程中都獲取這個全局變量
# num-=1
lock.acquire() #加同步鎖
temp=num
print('--get num:',num )
#time.sleep(0.1)
num =temp-1 #對此公共變量進行-1操作
lock.release() #解鎖
num = 100 #設定一個共享變量
thread_list = []
lock=threading.Lock() #創建lock對象
for i in range(100):
t = threading.Thread(target=addNum)
t.start()
thread_list.append(t)
for t in thread_list: #等待所有線程執行完畢
t.join() #所有線程執行完後主程序才能結束
print('final num:', num )
GIL與同步鎖的作用對比:
GIL:同一時刻只能有一個線程進入解釋器。
同步鎖:同一時刻,保證只有一個線程被執行,在局部保證操作共享資源時不會發生衝突。
八、線程死鎖和遞歸鎖
所謂死鎖: 是指兩個或兩個以上的進程在執行過程中,因爭奪資源而造成的一種互相等待的現象,若無外力作用,它們都將無法推進下去。此時稱系統處於死鎖狀態或系統產生了死鎖,這些永遠在互相等待的進程稱爲死鎖進程。 由於資源佔用是互斥的,當某個進程提出申請資源後,使得有關進程在無外力協助下,永遠分配不到必需的資源而無法繼續運行,這就產生了一種特殊現象死鎖。
實例:
import threading,time
class myThread(threading.Thread):
def doA(self):
lockA.acquire()
print(self.name,"gotlockA",time.ctime())
time.sleep(3)
lockB.acquire()
print(self.name,"gotlockB",time.ctime())
lockB.release()
lockA.release()
def doB(self):
lockB.acquire()
print(self.name,"gotlockB",time.ctime())
time.sleep(2)
lockA.acquire()
print(self.name,"gotlockA",time.ctime())
lockA.release()
lockB.release()
def run(self):
self.doA()
self.doB()
if __name__=="__main__":
lockA=threading.Lock()
lockB=threading.Lock()
threads=[]
for i in range(5):
threads.append(myThread())
for t in threads:
t.start()
for t in threads:
t.join()#等待線程結束,後面再講。
死鎖解決辦法:使用遞歸鎖,創建Rlock對象,在需要加鎖時使用
lockA=threading.Lock()
lockB=threading.Lock()
lock = threading.Rlock()