日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python中多線程和多處理的初學(xué)者指南

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

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、重慶小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了翼城免費(fèi)建站歡迎大家使用!

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

多線程

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

1. 沒有線程

在本例中,我們希望通過順序運(yùn)行程序來(lái)查看從Unsplash API下載15張圖像需要多長(zhǎng)時(shí)間:

 
 
  1. import requests
  2. import time
  3. img_urls = [
  4.     'https://images.unsplash.com/photo-1516117172878-fd2c41f4a759',
  5.     'https://images.unsplash.com/photo-1532009324734-20a7a5813719',
  6.     'https://images.unsplash.com/photo-1524429656589-6633a470097c',
  7.     'https://images.unsplash.com/photo-1530224264768-7ff8c1789d79',
  8.     'https://images.unsplash.com/photo-1564135624576-c5c88640f235',
  9.     'https://images.unsplash.com/photo-1541698444083-023c97d3f4b6',
  10.     'https://images.unsplash.com/photo-1522364723953-452d3431c267',
  11.     'https://images.unsplash.com/photo-1513938709626-033611b8cc03',
  12.     'https://images.unsplash.com/photo-1507143550189-fed454f93097',
  13.     'https://images.unsplash.com/photo-1493976040374-85c8e12f0c0e',
  14.     'https://images.unsplash.com/photo-1504198453319-5ce911bafcde',
  15.     'https://images.unsplash.com/photo-1530122037265-a5f1f91d3b99',
  16.     'https://images.unsplash.com/photo-1516972810927-80185027ca84',
  17.     'https://images.unsplash.com/photo-1550439062-609e1531270e',
  18.     'https://images.unsplash.com/photo-1549692520-acc6669e2f0c'
  19. ]
  20. start = time.perf_counter() #start timer
  21. for img_url in img_urls:
  22.     img_name = img_url.split('/')[3] #get image name from url
  23.     img_bytes = requests.get(img_url).content
  24. with open(img_name, 'wb') as img_file:
  25.      img_file.write(img_bytes) #save image to disk 
  26. finish = time.perf_counter() #end timer
  27. print(f"Finished in {round(finish-start,2)} seconds") 
  28. #results
  29. Finished in 23.101926751 seconds

一共用時(shí)?23秒。

2. 多線程

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

 
 
  1. import time
  2. from concurrent.futures import ThreadPoolExecutor
  3. def download_images(url):
  4.     img_name = img_url.split('/')[3]
  5.     img_bytes = requests.get(img_url).content
  6.     with open(img_name, 'wb') as img_file:
  7.          img_file.write(img_bytes)
  8.          print(f"{img_name} was downloaded")
  9. start = time.perf_counter() #start timer
  10. with ThreadPoolExecutor() as executor:
  11.     results = executor.map(download_images,img_urls) #this is Similar to map(func, *iterables)
  12. finish = time.perf_counter() #end timer
  13. print(f"Finished in {round(finish-start,2)} seconds")
  14. #results 
  15. Finished in 5.544147536 seconds

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

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

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


當(dāng)前文章:Python中多線程和多處理的初學(xué)者指南
網(wǎng)頁(yè)地址:http://www.5511xx.com/article/dpcchhs.html