久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

Python中多線程和多處理的指南是怎樣的

Python中多線程和多處理的指南是怎樣的,相信很多沒有經(jīng)驗(yàn)的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、成都做網(wǎng)站、湟源網(wǎng)絡(luò)推廣、小程序定制開發(fā)、湟源網(wǎng)絡(luò)營銷、湟源企業(yè)策劃、湟源品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供湟源建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:sd-ha.com

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

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

我們舉一個(gè)例子,編寫一個(gè)小的Python 腳本從Unsplash下載圖像。我們將從一次下載一個(gè)圖像的版本開始。接下來,我們使用線程來提高執(zhí)行速度。

Python中多線程和多處理

多線程

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

1. 沒有線程

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

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

一共用時(shí)23秒。

2. 多線程

讓我們看看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)建線程時(shí)存在開銷,因此將線程用于多個(gè)API調(diào)用是有意義的,而不僅僅是單個(gè)調(diào)用。

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

看完上述內(nèi)容,你們掌握Python中多線程和多處理的指南是怎樣的的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

新聞名稱:Python中多線程和多處理的指南是怎樣的
URL鏈接:http://sd-ha.com/article46/jgspeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、ChatGPT標(biāo)簽優(yōu)化、虛擬主機(jī)、用戶體驗(yàn)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)