python——threading模塊


一、什么是線程

線程是操作系統能夠進行運算調度的最小單位。進程被包含在進程中,是進程中實際處理單位。一條線程就是一堆指令集合。

一條線程是指進程中一個單一順序的控制流,一個進程中可以並發多個線程,每條線程並行執行不同的任務。

二、什么是進程

進程(Process)是計算機中的程序關於某數據集合上的一次運行活動,是系統進行資源分配和調度的基本單位,是操作系統結構的基礎。在早期面向進程設計的計算機結構中,進程是程序的基本執行實體;在當代面向線程設計的計算機結構中,進程是線程的容器。程序是指令、數據及其組織形式的描述,進程是程序的實體。

三、區別

進程(process)是一塊包含了某些資源的內存區域。操作系統利用進程把它的工作划分為一些功能單元。

進程中所包含的一個或多個執行單元稱為線程(thread)。進程還擁有一個私有的虛擬地址空間,該空間僅能被它所包含的線程訪問。

線程只能歸屬於一個進程並且它只能訪問該進程所擁有的資源。當操作系統創建一個進程后,該進程會自動申請一個名為主線程或首要線程的線程。

處理IO密集型任務或函數用線程;

處理計算密集型任務或函數用進程。

四、GIL

首先需要明確的一點是 GIL 並不是Python的特性,它是在實現Python解析器(CPython)時所引入的一個概念。就好比C++是一套語言(語法)標准,但是可以用不同的編譯器來編譯成可執行代碼。有名的編譯器例如GCC,INTEL C++,Visual C++等。Python也一樣,同樣一段代碼可以通過CPython,PyPy,Psyco等不同的Python執行環境來執行。像其中的JPython就沒有GIL。然而因為CPython是大部分環境下默認的Python執行環境。所以在很多人的概念里CPython就是Python,也就想當然的把 GIL 歸結為Python語言的缺陷。所以這里要先明確一點:GIL並不是Python的特性,Python完全可以不依賴於GIL

那么CPython實現中的GIL又是什么呢?GIL全稱 Global Interpreter Lock 為了避免誤導,我們還是來看一下官方給出的解釋:

In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

GIL限制:在同一時刻,只能有一個線程進入CPython解釋器。

解決GIL限制的辦法:

(1)Multiprocess

既然多線程不能同時進入Cpython解釋器,那么,我們可以通過把多個線程放入不同進程中,讓多進程進入Cpython解釋器,分配給各個CUP,以利用多核實現並行。

(2)C

五、創建線程

導入模塊threading,通過threading.Thread()創建線程。其中target接收的是要執行的函數名字,args接收傳入函數的參數,以元組()的形式表示。

1 import threading
2 
3 def foo(n)
4     print("foo%s"%n)
5 t1 = threading.Thread(target=foo,args=(1,))  #創建線程對象

六、啟動線程

通過線程對象t1.start()或t2.start()啟動線程。

1 t1 = threading.Thread(target=sayhi, args=(1,))  # 生成一個線程實例
2 t2 = threading.Thread(target=sayhi, args=(2,))  # 生成另一個線程實例
3 
4 t1.start()  # 啟動線程
5 t2.start()  # 啟動另一個線程

 

實例1:

 1 import threading
 2 import time
 3 
 4 def foo(n):
 5     print("foo%s"%n)
 6     time.sleep(1)
 7 
 8 def bar(n):
 9     print("bar%s"%n)
10     time.sleep(2)
11 
12 t1 = threading.Thread(target=foo,args=(1,))
13 t2 = threading.Thread(target=bar,args=(2,))
14 
15 t1.start()
16 t2.start()
17 
18 print("...in the main...")
View Code

結果:

程序啟動后,主線程從上到下依次執行,t1、t2兩個子線程啟動后,與主線程並行,搶占CPU資源。因此,前三行的輸出結果幾乎同時打印,沒有先后順序,此時,需要等t1和t2都結束后程序才結束。故等待2s后,程序結束。程序總共花了2s。

foo1
bar2
...in the main...

Process finished with exit code 0

實例2:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正傳',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27 
28     print ("all over %s" %ctime())
View Code

結果:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正傳',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27         t.join()
28 
29     print ("all over %s" %ctime())
View Code

結果解析:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正傳',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27     t.join()        #for循環的最后一次t的值,相當於t2
28 
29     print ("all over %s" %ctime())
View Code

結果:

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:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正傳',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.start()
27     t1.join()        #當t1調用join()時
28 
29     print ("all over %s" %ctime())
View Code

結果:

1 Begin listening to 七里香. Thu Sep 29 15:35:35 2016    #t1和t2啟動
2 Begin watching at the 阿甘正傳! Thu Sep 29 15:35:35 2016
3 end listening Thu Sep 29 15:35:39 2016
4 Begin listening to 七里香. Thu Sep 29 15:35:39 2016
5 end watching Thu Sep 29 15:35:40 2016
6 Begin watching at the 阿甘正傳! Thu Sep 29 15:35:40 2016
7 end listening Thu Sep 29 15:35:43 2016    #t1結束,主線程繼續往下執行
8 all over Thu Sep 29 15:35:43 2016      #主線程結束
9 end watching Thu Sep 29 15:35:45 2016    #t2結束

八、setDaemon(True)

將線程聲明為守護線程,必須在start() 方法調用之前設置, 如果不設置為守護線程程序會被無限掛起。這個方法基本和join是相反的。當我們 在程序運行中,執行一個主線程,如果主線程又創建一個子線程,主線程和子線程 就兵分兩路,分別運行,那么當主線程完成想退出時,會檢驗子線程是否完成。如 果子線程未完成,則主線程會等待子線程完成后再退出。但是有時候我們需要的是 只要主線程完成了,不管子線程是否完成,都要和主線程一起退出,這時就可以用setDaemon方法。

實例:

 1 import threading
 2 from time import ctime,sleep
 3 import time
 4 
 5 def music(func):
 6     for i in range(2):
 7         print ("Begin listening to %s. %s" %(func,ctime()))
 8         sleep(4)
 9         print("end listening %s"%ctime())
10 
11 def move(func):
12     for i in range(2):
13         print ("Begin watching at the %s! %s" %(func,ctime()))
14         sleep(5)
15         print('end watching %s'%ctime())
16 
17 threads = []
18 t1 = threading.Thread(target=music,args=('七里香',))
19 threads.append(t1)
20 t2 = threading.Thread(target=move,args=('阿甘正傳',))
21 threads.append(t2)
22 
23 if __name__ == '__main__':
24 
25     for t in threads:
26         t.setDaemon(True)
27         t.start()
28 
29 
30     print ("all over %s" %ctime())
View Code

結果:

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()

獲取當前進程的名稱。

 

十、同步鎖

 1 import time
 2 import threading
 3 
 4 def addNum():
 5     global num #在每個線程中都獲取這個全局變量  6     # num-=1
 7 
 8     temp=num
 9     print('--get num:',num )
10     #time.sleep(0.1)
11     num =temp-1 #對此公共變量進行-1操作
12 
13 
14 num = 100  #設定一個共享變量
15 thread_list = []
16 for i in range(100):
17     t = threading.Thread(target=addNum)
18     t.start()
19     thread_list.append(t)
20 
21 for t in thread_list: #等待所有線程執行完畢 22     t.join()
23 
24 print('final num:', num )

用num -= 1則最終結果沒問題,這是因為完成這個操作太快了,在線程切換時間內。用中間變量temp進行賦值時出現問題,這是因為100個線程,每一個都沒有執行完就就行了切換,因此最終得到的不是0。

多個線程同時操作同一個共享資源,所以導致沖突,這種情況就需要用同步鎖來解決。

 1 import time
 2 import threading
 3 
 4 def addNum():
 5     global num #在每個線程中都獲取這個全局變量  6     # num-=1
 7     lock.acquire()    #加同步鎖  8     temp=num
 9     print('--get num:',num )
10     #time.sleep(0.1)
11     num =temp-1 #對此公共變量進行-1操作
12     lock.release()    #解鎖 13 
14 num = 100  #設定一個共享變量
15 thread_list = []
16 lock=threading.Lock()  #創建lock對象 17 
18 for i in range(100):
19     t = threading.Thread(target=addNum)
20     t.start()
21     thread_list.append(t)
22 
23 for t in thread_list: #等待所有線程執行完畢 24     t.join()      #所有線程執行完后主程序才能結束 25 
26 print('final num:', num )

GIL與同步鎖的作用對比:

GIL:同一時刻只能有一個線程進入解釋器。

同步鎖:同一時刻,保證只有一個線程被執行,在局部保證操作共享資源時不會發生沖突。

 

十一、線程死鎖和遞歸鎖

所謂死鎖: 是指兩個或兩個以上的進程在執行過程中,因爭奪資源而造成的一種互相等待的現象,若無外力作用,它們都將無法推進下去。此時稱系統處於死鎖狀態或系統產生了死鎖,這些永遠在互相等待的進程稱為死鎖進程。 由於資源占用是互斥的,當某個進程提出申請資源后,使得有關進程在無外力協助下,永遠分配不到必需的資源而無法繼續運行,這就產生了一種特殊現象死鎖。

實例:

 1 import threading,time
 2 
 3 class myThread(threading.Thread):
 4     def doA(self):
 5         lockA.acquire()
 6         print(self.name,"gotlockA",time.ctime())
 7         time.sleep(3)
 8         lockB.acquire()
 9         print(self.name,"gotlockB",time.ctime())
10         lockB.release()
11         lockA.release()
12 
13     def doB(self):
14         lockB.acquire()
15         print(self.name,"gotlockB",time.ctime())
16         time.sleep(2)
17         lockA.acquire()
18         print(self.name,"gotlockA",time.ctime())
19         lockA.release()
20         lockB.release()
21     def run(self):
22         self.doA()
23         self.doB()
24 if __name__=="__main__":
25 
26     lockA=threading.Lock()
27     lockB=threading.Lock()
28     threads=[]
29     for i in range(5):
30         threads.append(myThread())
31     for t in threads:
32         t.start()
33     for t in threads:
34         t.join()#等待線程結束,后面再講。
View Code

死鎖解決辦法:

使用遞歸鎖,創建Rlock對象,在需要加鎖時使用

1 lockA=threading.Lock()
2 lockB=threading.Lock()


lock = threading.Rlock()

 


免責聲明!

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



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