多執行緒概述
多執行緒使得程式內部可以分出多個執行緒來做多件事情,充分利用CPU空閒時間,提升處理效率。python提供了兩個模組來實現多執行緒thread 和threading ,thread 有一些缺點,在threading 得到了彌補。並且在Python3中廢棄了thread模組,保留了更強大的threading模組。
使用場景
在python的原始直譯器CPython中存在著GIL(Global Interpreter Lock,全域性直譯器鎖),因此在解釋執行python程式碼時,會產生互斥鎖來限制執行緒對共享資源的訪問,直到直譯器遇到I/O操作或者操作次數達到一定數目時才會釋放GIL。所以,雖然CPython的執行緒庫直接封裝了系統的原生執行緒,但CPython整體作為一個程式,同一時間只會有一個獲得GIL的執行緒在跑,其他執行緒則處於等待狀態。這就造成了即使在多核CPU中,多執行緒也只是做著分時切換而已。
如果你的程式是CPU密集型,多個執行緒的程式碼很有可能是線性執行的。所以這種情況下多執行緒是雞肋,效率可能還不如單執行緒因為有上下文切換開銷。但是如果你的程式碼是IO密集型,涉及到網路、磁碟IO的任務都是IO密集型任務,多執行緒可以明顯提高效率,例如多執行緒爬蟲,多執行緒檔案處理等等
多執行緒爬蟲
多執行緒爬蟲的程式碼例項
注: 以下程式碼在python3下執行透過, python2版本差異較大,不能執行成功,如需幫助請下方留意。
# coding=utf-8 import threading, queue, time, urllib from urllib import request baseUrl = 'http://www.pythontab.com/html/pythonjichu/' urlQueue = queue.Queue() for i in range(2, 10): url = baseUrl + str(i) + '.html' urlQueue.put(url) #print(url) def fetchUrl(urlQueue): while True: try: #不阻塞的讀取佇列資料 url = urlQueue.get_nowait() i = urlQueue.qsize() except Exception as e: break print ('Current Thread Name %s, Url: %s ' % (threading.currentThread().name, url)) try: response = urllib.request.urlopen(url) responseCode = response.getcode() except Exception as e: continue if responseCode == 200: #抓取內容的資料處理可以放到這裡 #為了突出效果, 設定延時 time.sleep(1) if __name__ == '__main__': startTime = time.time() threads = [] # 可以調節執行緒數, 進而控制抓取速度 threadNum = 4 for i in range(0, threadNum): t = threading.Thread(target=fetchUrl, args=(urlQueue,)) threads.append(t) for t in threads: t.start() for t in threads: #多執行緒多join的情況下,依次執行各執行緒的join方法, 這樣可以確保主執行緒最後退出, 且各個執行緒間沒有阻塞 t.join() endTime = time.time() print ('Done, Time cost: %s ' % (endTime - startTime))
執行結果:
1個執行緒時:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/8.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Done, Time cost: 8.182249069213867
2個執行緒時:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/8.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Done, Time cost: 4.0987958908081055
3個執行緒時:
Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/2.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/3.html Current Thread Name Thread-3, Url: http://www.pythontab.com/html/pythonjichu/4.html Current Thread Name Thread-4, Url: http://www.pythontab.com/html/pythonjichu/5.html Current Thread Name Thread-2, Url: http://www.pythontab.com/html/pythonjichu/6.html Current Thread Name Thread-4, Url: http://www.pythontab.com/html/pythonjichu/7.html Current Thread Name Thread-1, Url: http://www.pythontab.com/html/pythonjichu/9.html Current Thread Name Thread-3, Url: http://www.pythontab.com/html/pythonjichu/8.html Done, Time cost: 2.287320137023926
透過調節執行緒數可以看到,執行時間會隨著執行緒數的增加而縮短,抓取效率成正比增加。
總結:
Python多執行緒在IO密集型任務,多執行緒可以明顯提高效率,CPU密集型任務不適合使用多執行緒處理。