天天看點

Python 單線程與多線程批量下載下傳的比較

目前剛學習了Python,想要自己試試爬蟲下載下傳,就看了《Python核心程式設計》這本書,和綜合了網上很多的爬蟲下載下傳的代碼,是以自己來試試。BTW:我用的是python3.6.

這是單線程下載下傳30個url:

from urllib.request import urlretrieve

import time

import random

start=time.time()

f=open('E:\Python\py\web\hh.txt','r')#打開存放URL的檔案

a=f.readlines()

f.close()

for i in a:

    b=random.randint(0,30)

    urlretrieve(i,'%d.png'%b) 

end=time.time()

print(end-start)

輸出時間是:4.2432427406311035

同樣的url檔案,我用多線程和隊列來實作:

from urllib.request import urlretrieve

import queue  

import threading   

import random

import time

class download(threading.Thread):  

    def __init__(self,que):  

        threading.Thread.__init__(self)  

        self.que=que  

    def run(self):  

        while True:  

            if not self.que.empty():   

                host=self.que.get()

                a=random.randint(0,30)

                urlretrieve(host,'%d.png'%a) 

            else:  

                break  

def Down():  

    f=open('E:\Python\py\web\hh.txt','r')

    a=f.readlines()

    f.close()

    que=queue.Queue() 

    threads=[]

    for i in a:  

        que.put(i)  

    for i in range(20):  

        d=download(que) 

        threads.append(d)

    for i in threads:

        i.start()

    for i in threads:

        i.join()

if __name__=='__main__':   

    start=time.time()

    Down()  

    end=time.time()

    print(end-start)

最後輸出是:3.6262073516845703          可以看出多線程是快了一點的

在最後我還想試試用線程池來試試,但發現我下載下傳的Anaconda3上沒有threadpool這個子產品,就之後再試了

第一次寫部落格,想想都有點激動