python筆記13-多線程實戰篇(tomorrow)

來源:互聯網
上載者:User

標籤:筆記   代碼   pytho   file   turn   檔案   os.path   jin   data   

安裝

1.tomorrow安裝,用pip可以直接安裝

pip install tomorrow

單線程

1.以下案例是單線程時候跑的情況,在下載圖片的時候很耗時。

# coding:utf-8from bs4 import BeautifulSoupimport requestsimport osimport time# 當前指令碼所在的目錄cur_path = os.path.dirname(os.path.realpath(__file__))def get_img_urls():    r = requests.get("http://699pic.com/sousuo-218808-13-1.html")    fengjing = r.content    soup = BeautifulSoup(fengjing, "html.parser")    # 找出所有的標籤    images = soup.find_all(class_="lazy")    return imagesdef save_img(imgUrl):    try:        jpg_rl = imgUrl["data-original"]        title = imgUrl["title"]        # print(title)        # print(jpg_rl)        # print("")        # 判斷是否有jpg檔案夾,不存在建立一個        save_file = os.path.join(cur_path, "jpg")        if not os.path.exists(save_file): os.makedirs(save_file)        with open(os.path.join(save_file, title+‘.jpg‘), "wb") as f:            f.write(requests.get(jpg_rl).content)    except:        passif __name__ == "__main__":    t1 = time.time()    image_ulrs = get_img_urls()    for i in image_ulrs:        save_img(i)    t2 = time.time()    print("總耗時:%.2f 秒"%(t2-t1))

 

運行結果:

耗時:4.27 秒
使用多線程tomorrow

1.一行代碼搞定多線程,在函數上加個@threads(5),括弧裡面代碼線程的數量,數字越大,啟動並執行速度越快

# coding:utf-8from bs4 import BeautifulSoupimport requestsimport osimport timefrom tomorrow import threads# 當前指令碼所在的目錄cur_path = os.path.dirname(os.path.realpath(__file__))def get_img_urls():    r = requests.get("http://699pic.com/sousuo-218808-13-1.html")    fengjing = r.content    soup = BeautifulSoup(fengjing, "html.parser")    # 找出所有的標籤    images = soup.find_all(class_="lazy")    return images@threads(5)def save_img(imgUrl):    try:        jpg_rl = imgUrl["data-original"]        title = imgUrl["title"]        # print(title)        # print(jpg_rl)        # print("")        # 判斷是否有jpg檔案夾,不存在建立一個        save_file = os.path.join(cur_path, "jpg")        if not os.path.exists(save_file): os.makedirs(save_file)        with open(os.path.join(save_file, title+‘.jpg‘), "wb") as f:            f.write(requests.get(jpg_rl).content)    except:        passif __name__ == "__main__":    t1 = time.time()    image_ulrs = get_img_urls()    for i in image_ulrs:        save_img(i)    t2 = time.time()    print("總耗時:%.2f 秒"%(t2-t1))

 

運行結果:

總耗時:0.24 秒

參考github案例:Tomorrow

python筆記13-多線程實戰篇(tomorrow)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.