python3爬蟲中多線程的優(yōu)勢總結(jié)
有些小伙伴跟小編討論了python中使用多線程原理的問題,就聊到了關(guān)于python多線程的弊端問題,這點可能在使用的過程中大家會能感覺到。而且之前講過的GIL也是對python多線程的一種限制。那么,我們?yōu)槭裁催€要用多線程呢?當然是多線程的優(yōu)勢已經(jīng)掩蓋了它本身不足之處,所以我們來加強一下學習python多線程的信心吧~
總結(jié)起來,使用多線程編程具有如下幾個優(yōu)點:
進程之間不能共享內(nèi)存,但線程之間共享內(nèi)存非常容易。
操作系統(tǒng)在創(chuàng)建進程時,需要為該進程重新分配系統(tǒng)資源,但創(chuàng)建線程的代價則小得多。因此,使用多線程來實現(xiàn)多任務(wù)并發(fā)執(zhí)行比使用多進程的效率高。
Python 語言內(nèi)置了多線程功能支持,而不是單純地作為底層操作系統(tǒng)的調(diào)度方式,從而簡化了 Python 的多線程編程。
threading模塊
普通創(chuàng)建方式
import threading
import time
def run(n):
print("task", n)
time.sleep(1)
print('2s')
time.sleep(1)
print('1s')
time.sleep(1)
print('0s')
time.sleep(1)
if __name__ == '__main__':
t1 = threading.Thread(target=run, args=("t1",))
t2 = threading.Thread(target=run, args=("t2",))
t1.start()
t2.start()
----------------------------------
>>> task t1
>>> task t2
>>> 2s
>>> 2s
>>> 1s
>>> 1s
>>> 0s
>>> 0s
守護線程
我們看下面這個例子,這里使用setDaemon(True)把所有的子線程都變成了主線程的守護線程,因此當主進程結(jié)束后,子線程也會隨之結(jié)束。所以當主線程結(jié)束后,整個程序就退出了。
import threading
import time
def run(n):
print("task", n)
time.sleep(1) #此時子線程停1s
print('3')
time.sleep(1)
print('2')
time.sleep(1)
print('1')
if __name__ == '__main__':
t = threading.Thread(target=run, args=("t1",))
t.setDaemon(True) #把子進程設(shè)置為守護線程,必須在start()之前設(shè)置
t.start()
print("end")
----------------------------------
>>> task t1
>>> end
我們可以發(fā)現(xiàn),設(shè)置守護線程之后,當主線程結(jié)束時,子線程也將立即結(jié)束,不再執(zhí)行。
python多線程實例代碼:
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
到此這篇關(guān)于python3爬蟲中多線程的優(yōu)勢總結(jié)的文章就介紹到這了,更多相關(guān)python3爬蟲中多線程的優(yōu)勢有哪些內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python基礎(chǔ)進階之海量表情包多線程爬蟲功能的實現(xiàn)
- python3爬蟲中多線程進行解鎖操作實例
- python3爬蟲GIL修改多線程實例講解
- python爬蟲開發(fā)之使用Python爬蟲庫requests多線程抓取貓眼電影TOP100實例
- python支持多線程的爬蟲實例
- python爬蟲中多線程的使用詳解
- Python 微信爬蟲完整實例【單線程與多線程】
- python面向?qū)ο蠖嗑€程爬蟲爬取搜狐頁面的實例代碼
- python爬蟲爬取快手視頻多線程下載功能
- Python3多線程爬蟲實例講解代碼
- php與python實現(xiàn)的線程池多線程爬蟲功能示例
- python 多線程爬取壁紙網(wǎng)站的示例
相關(guān)文章
Python中創(chuàng)建字典的幾種方法總結(jié)(推薦)
下面小編就為大家?guī)硪黄狿ython中創(chuàng)建字典的幾種方法總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
matplotlib之pyplot模塊之標題(title()和suptitle())
這篇文章主要介紹了matplotlib之pyplot模塊之標題(title()和suptitle()),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
pandas如何將datetime64[ns]轉(zhuǎn)為字符串日期
這篇文章主要介紹了pandas如何將datetime64[ns]轉(zhuǎn)為字符串日期,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Tensorflow訓練MNIST手寫數(shù)字識別模型
這篇文章主要為大家詳細介紹了Tensorflow訓練MNIST手寫數(shù)字識別模型,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02
python中使用ctypes調(diào)用so傳參設(shè)置遇到的問題及解決方法
這篇文章主要介紹了python中使用ctypes調(diào)用so傳參設(shè)置,本文較詳細的給大家介紹了遇到問題及解決方案,需要的朋友可以參考下2019-06-06
Python range與enumerate函數(shù)區(qū)別解析
這篇文章主要介紹了Python range與enumerate函數(shù)區(qū)別解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02

