成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python中多線程和多處理的初學者指南

開發 后端
使用Python分析數據,如果使用了正確的數據結構和算法,有時可以大量提高程序的速度。實現此目的的一種方法是使用Muiltithreading(多線程)或Multiprocessing(多重處理)。

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

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

[[324279]]

多線程

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

1. 沒有線程

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

  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.  
  27. finish = time.perf_counter() #end timer 
  28. print(f"Finished in {round(finish-start,2)} seconds")  
  29.  
  30. #results 
  31. Finished in 23.101926751 seconds 

一共用時​23秒。

2. 多線程

讓我們看看Pyhton中的線程模塊如何顯著地改進我們的程序執行:

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

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

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

此外,對于密集的計算,如數據處理,圖像處理多處理比線程執行得更好。

責任編輯:趙寧寧 來源: 今日頭條
相關推薦

2009-11-13 15:46:25

Java多線程

2022-04-24 15:21:01

MarkdownHTML

2022-07-22 13:14:57

TypeScript指南

2010-06-13 11:13:38

UML初學者指南

2022-10-10 15:28:45

負載均衡

2021-05-10 08:50:32

網絡管理網絡網絡性能

2023-07-28 07:31:52

JavaScriptasyncawait

2022-03-28 09:52:42

JavaScript語言

2023-07-03 15:05:07

預測分析大數據

2022-09-05 15:36:39

Linux日志記錄syslogd

2018-10-28 16:14:55

Reactreact.js前端

2010-08-26 15:47:09

vsftpd安裝

2023-02-10 08:37:28

2012-03-14 10:56:23

web app

2023-04-26 13:49:52

2023-10-16 07:04:03

2024-12-25 08:00:00

機器學習ML管道人工智能

2021-05-06 09:00:00

JavaScript靜態代碼開發

2014-04-01 10:20:00

開源Rails

2020-08-16 13:10:46

TensorFlow深度學習數據集
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲国产中文字幕 | 日韩和的一区二区 | 亚洲欧美一区二区三区在线 | 亚洲欧美日韩精品久久亚洲区 | 欧美aaaa视频 | 国产真实精品久久二三区 | 国产日产欧产精品精品推荐蛮挑 | 亚洲男女激情 | 国产在线精品一区二区三区 | 成人精品啪啪欧美成 | 成人免费在线视频 | 精品国产91久久久久久 | 亚洲欧美视频一区 | 中文在线亚洲 | 欧美视频一区二区三区 | 夜夜爽99久久国产综合精品女不卡 | 99久久婷婷国产综合精品电影 | 中文字幕视频在线看 | 久久尤物免费一区二区三区 | 一区二区三区电影网 | 在线男人天堂 | 福利在线看 | 久久91 | 9999久久| 亚洲美女天堂网 | cao在线| 亚洲a视 | 精品免费国产一区二区三区四区介绍 | 中文字幕一区二区三区四区 | 亚洲午夜精品视频 | 国际精品鲁一鲁一区二区小说 | 欧美日韩在线一区 | 精品视频一区二区三区四区 | 久久一级免费视频 | 7777精品伊人久久精品影视 | 亚洲国产欧美国产综合一区 | 麻豆亚洲 | 国产精品久久久久久久久久久久 | 国产毛片毛片 | 成人亚洲网 | 日日夜夜操天天干 |