溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Python3多線程爬蟲(chóng)實(shí)例講解代碼

發(fā)布時(shí)間:2020-10-17 03:56:13 來(lái)源:腳本之家 閱讀:378 作者:pythontab 欄目:開(kāi)發(fā)技術(shù)

多線程概述

多線程使得程序內(nèi)部可以分出多個(gè)線程來(lái)做多件事情,充分利用CPU空閑時(shí)間,提升處理效率。python提供了兩個(gè)模塊來(lái)實(shí)現(xiàn)多線程thread 和threading ,thread 有一些缺點(diǎn),在threading 得到了彌補(bǔ)。并且在Python3中廢棄了thread模塊,保留了更強(qiáng)大的threading模塊。

使用場(chǎng)景

在python的原始解釋器CPython中存在著GIL(Global Interpreter Lock,全局解釋器鎖),因此在解釋執(zhí)行python代碼時(shí),會(huì)產(chǎn)生互斥鎖來(lái)限制線程對(duì)共享資源的訪問(wèn),直到解釋器遇到I/O操作或者操作次數(shù)達(dá)到一定數(shù)目時(shí)才會(huì)釋放GIL。所以,雖然CPython的線程庫(kù)直接封裝了系統(tǒng)的原生線程,但CPython整體作為一個(gè)進(jìn)程,同一時(shí)間只會(huì)有一個(gè)獲得GIL的線程在跑,其他線程則處于等待狀態(tài)。這就造成了即使在多核CPU中,多線程也只是做著分時(shí)切換而已。

如果你的程序是CPU密集型,多個(gè)線程的代碼很有可能是線性執(zhí)行的。所以這種情況下多線程是雞肋,效率可能還不如單線程因?yàn)橛猩舷挛那袚Q開(kāi)銷(xiāo)。但是如果你的代碼是IO密集型,涉及到網(wǎng)絡(luò)、磁盤(pán)IO的任務(wù)都是IO密集型任務(wù),多線程可以明顯提高效率,例如多線程爬蟲(chóng),多線程文件處理等等

多線程爬蟲(chóng)

多線程爬蟲(chóng)的代碼實(shí)例

注: 以下代碼在python3下運(yùn)行通過(guò), python2版本差異較大,不能運(yùn)行成功,如需幫助請(qǐng)下方留意。

# 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:
   #不阻塞的讀取隊(duì)列數(shù)據(jù)
   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:
   #抓取內(nèi)容的數(shù)據(jù)處理可以放到這里
   #為了突出效果, 設(shè)置延時(shí)
   time.sleep(1)
if __name__ == '__main__':
 startTime = time.time()
 threads = []
 # 可以調(diào)節(jié)線程數(shù), 進(jìn)而控制抓取速度
 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的情況下,依次執(zhí)行各線程的join方法, 這樣可以確保主線程最后退出, 且各個(gè)線程間沒(méi)有阻塞
  t.join()
 endTime = time.time()
 print ('Done, Time cost: %s ' % (endTime - startTime))

運(yùn)行結(jié)果:

1個(gè)線程時(shí):

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個(gè)線程時(shí):

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個(gè)線程時(shí):

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

通過(guò)調(diào)節(jié)線程數(shù)可以看到,執(zhí)行時(shí)間會(huì)隨著線程數(shù)的增加而縮短,抓取效率成正比增加。

總結(jié):

Python多線程在IO密集型任務(wù),多線程可以明顯提高效率,CPU密集型任務(wù)不適合使用多線程處理。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI