亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

使用Python分析數(shù)據(jù),如果使用了正確的數(shù)據(jù)結(jié)構(gòu)和算法,有時可以大量提高程序的速度。實現(xiàn)此目的的一種方法是使用Muiltithreading(多線程)或Multiprocessing(多重處理)。

在這篇文章中,我們不會詳細(xì)討論多線程或多處理的內(nèi)部原理。相反,我們舉一個例子,編寫一個小的Python腳本從Unsplash下載圖像。我們將從一次下載一個圖像的版本開始。接下來,我們使用線程來提高執(zhí)行速度。

 

Python中多線程和多處理的初學(xué)者指南

 

多線程

簡單地說,線程允許您并行地運行程序。花費大量時間等待外部事件的任務(wù)通常適合線程化。它們也稱為I/O Bound任務(wù)例如從文件中讀寫,網(wǎng)絡(luò)操作或使用API在線下載。讓我們來看一個示例,它展示了使用線程的好處。

 

沒有線程

在本例中,我們希望通過順序運行程序來查看從Unsplash API下載15張圖像需要多長時間:

import requests
import time
img_urls = [
    'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759',
    'https://images.unsplash.com/photo-1532009324734-20a7a5813719',
    'https://images.unsplash.com/photo-1524429656589-6633a470097c',
    'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79',
    'https://images.unsplash.com/photo-1564135624576-c5c88640f235',
    'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6',
    'https://images.unsplash.com/photo-1522364723953-452d3431c267',
    'https://images.unsplash.com/photo-1513938709626-033611b8cc03',
    'https://images.unsplash.com/photo-1507143550189-fed454f93097',
    'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
    'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
    'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
    'https://images.unsplash.com/photo-1516972810927-80185027ca84',
    'https://images.unsplash.com/photo-1550439062-609e1531270e',
    'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
]

start = time.perf_counter() #start timer
for img_url in img_urls:
    img_name = img_url.split('/')[3] #get image name from url
    img_bytes = requests.get(img_url).content
with open(img_name, 'wb') as img_file:
     img_file.write(img_bytes) #save image to disk 

finish = time.perf_counter() #end timer
print(f"Finished in {round(finish-start,2)} seconds") 

#results
Finished in 23.101926751 seconds

一共用時?23秒。

 

多線程

 

讓我們看看Pyhton中的線程模塊如何顯著地改進(jìn)我們的程序執(zhí)行:

import time
from concurrent.futures import ThreadPoolExecutor

def download_images(url):
    img_name = img_url.split('/')[3]
    img_bytes = requests.get(img_url).content
    with open(img_name, 'wb') as img_file:
         img_file.write(img_bytes)
         print(f"{img_name} was downloaded")

start = time.perf_counter() #start timer
with ThreadPoolExecutor() as executor:
    results = executor.map(download_images,img_urls) #this is Similar to map(func, *iterables)
finish = time.perf_counter() #end timer
print(f"Finished in {round(finish-start,2)} seconds")

#results 
Finished in 5.544147536 seconds

我們可以看到,與不使用線程代碼相比,使用線程代碼可以顯著提高速度。從23秒到5秒。

對于本例,請注意在創(chuàng)建線程時存在開銷,因此將線程用于多個API調(diào)用是有意義的,而不僅僅是單個調(diào)用。

此外,對于密集的計算,如數(shù)據(jù)處理,圖像處理多處理比線程執(zhí)行得更好。

 

參考:https://dev.to/mojemoron/a-beginners-guide-to-multithreading-and-multiprocessing-in-python-part-1-n6h

分享到:
標(biāo)簽:多線程 Python
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達(dá)人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定