日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python创建多线程_初学者看过来:Python中多线程和多处理的指南

發布時間:2025/3/12 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python创建多线程_初学者看过来:Python中多线程和多处理的指南 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

在這篇文章中,我們不會詳細討論多線程或多處理的內部原理。相反,我們舉一個例子,編寫一個小的Python

腳本從Unsplash下載圖像。我們將從一次下載一個圖像的版本開始。接下來,我們使用線程來提高執行速度。

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

多線程

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

1. 沒有線程

在本例中,我們希望通過順序運行程序來查看從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秒。

2. 多線程

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

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秒。

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

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

總結

以上是生活随笔為你收集整理的python创建多线程_初学者看过来:Python中多线程和多处理的指南的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。