Python 多線程抓取圖片效率對比
更新時(shí)間:2016年02月27日 09:27:05 投稿:hebedich
Python由于有全鎖局的存在,并不能利用多核優(yōu)勢。所以,如果你的多線程進(jìn)程是CPU密集型的,那多線程并不能帶來效率上的提升,相反還可能會(huì)因?yàn)榫€程的頻繁切換,導(dǎo)致效率下降;如果是IO密集型,多線程進(jìn)程可以利用IO阻塞等待時(shí)的空閑時(shí)間執(zhí)行其他線程,提升效率。
目的:
是學(xué)習(xí)python 多線程的工作原理,及通過抓取400張圖片這種IO密集型應(yīng)用來查看多線程效率對比
import requests
import urlparse
import os
import time
import threading
import Queue
path = '/home/lidongwei/scrapy/owan_img_urls.txt'
#path = '/home/lidongwei/scrapy/cc.txt'
fetch_img_save_path = '/home/lidongwei/scrapy/owan_imgs/'
# 讀取保存再文件里面400個(gè)urls
with open(path) as f :
urls = f.readlines()
urls = urls[:400]
# 使用Queue來線程通信,因?yàn)殛?duì)列是線程安全的(就是默認(rèn)這個(gè)隊(duì)列已經(jīng)有鎖)
q = Queue.Queue()
for url in urls:
q.put(url)
start = time.time()
def fetch_img_func(q):
while True:
try:
# 不阻塞的讀取隊(duì)列數(shù)據(jù)
url = q.get_nowait()
i = q.qsize()
except Exception, e:
print e
break;
print 'Current Thread Name Runing %s ... 11' % threading.currentThread().name
url = url.strip()
img_path = urlparse.urlparse(url).path
ext = os.path.splitext(img_path)[1]
print 'handle %s pic... pic url %s ' % (i, url)
res = requests.get(url, stream=True)
if res.status_code == 200:
save_img_path = '%s%s%s' % (fetch_img_save_path, i, ext)
# 保存下載的圖片
with open(save_img_path, 'wb') as fs:
for chunk in res.iter_content(1024):
fs.write(chunk)
print 'save %s pic ' % i
# 可以開多個(gè)線程測試不同效果
t1 = threading.Thread(target=fetch_img_func, args=(q, ), name="child_thread_1")
#t2 = threading.Thread(target=fetch_img_func, args=(q, ), name="child_thread_2")
#t3 = threading.Thread(target=fetch_img_func, args=(q, ), name="child_thread_3")
#t4 = threading.Thread(target=fetch_img_func, args=(q, ), name="child_thread_4")
t1.start()
#t2.start()
#t3.start()
#t4.start()
t1.join()
#t2.join()
#t3.join()
#t4.join()
end = time.time()
print 'Done %s ' % (end-start)
實(shí)驗(yàn)結(jié)果
400圖片
4線程 Done 12.443133831 3線程 Done 12.9201757908 2線程 Done 32.8628299236 1線程 Done 54.6115460396
總結(jié)
Python 自帶GIL 大鎖, 沒有真正意義上的多線程并行執(zhí)行。GIL 大鎖會(huì)在線程阻塞的時(shí)候釋放,此時(shí)等待的線程就可以激活工作,這樣如此類推,大大提高IO阻塞型應(yīng)用的效率。
相關(guān)文章
在Python中使用AOP實(shí)現(xiàn)Redis緩存示例
本篇文章主要介紹了在Python中使用AOP實(shí)現(xiàn)Redis緩存示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例
今天小編就為大家分享一篇關(guān)于PyQt5內(nèi)嵌瀏覽器注入JavaScript腳本實(shí)現(xiàn)自動(dòng)化操作的代碼實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Python實(shí)現(xiàn)投影法分割圖像示例(一)
今天小編就為大家分享一篇Python實(shí)現(xiàn)投影法分割圖像示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

