Python?Concurrent?Futures解鎖并行化編程的魔法示例
基礎(chǔ)概念
ThreadPoolExecutor和ProcessPoolExecutor
concurrent.futures提供了兩個(gè)主要的執(zhí)行器:ThreadPoolExecutor和ProcessPoolExecutor。前者在單個(gè)進(jìn)程中使用多線程執(zhí)行任務(wù),而后者則在多個(gè)進(jìn)程中執(zhí)行,利用多核心資源。
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
# 使用ThreadPoolExecutor
with ThreadPoolExecutor() as executor:
results = executor.map(some_function, data)
# 使用ProcessPoolExecutor
with ProcessPoolExecutor() as executor:
results = executor.map(some_function, data)
Future對(duì)象
Future是異步計(jì)算的結(jié)果的占位符,表示一個(gè)可能在未來(lái)完成的操作。通過(guò)submit方法提交任務(wù)后,會(huì)返回一個(gè)Future對(duì)象,可以通過(guò)它獲取任務(wù)的狀態(tài)和結(jié)果。
from concurrent.futures import ThreadPoolExecutor
def some_function(data):
# 一些耗時(shí)的操作
return result
with ThreadPoolExecutor() as executor:
future = executor.submit(some_function, data)
result = future.result()并行化任務(wù)執(zhí)行
map方法
Executor對(duì)象的map方法可以方便地并行執(zhí)行函數(shù),并返回結(jié)果。
from concurrent.futures import ThreadPoolExecutor
def square(x):
return x * x
data = [1, 2, 3, 4, 5]
with ThreadPoolExecutor() as executor:
results = executor.map(square, data)
for result in results:
print(result)submit方法和as_completed函數(shù)
使用submit方法可以異步地提交任務(wù),而as_completed函數(shù)可以按完成順序迭代Future對(duì)象。
from concurrent.futures import ThreadPoolExecutor, as_completed
def square(x):
return x * x
data = [1, 2, 3, 4, 5]
with ThreadPoolExecutor() as executor:
futures = [executor.submit(square, x) for x in data]
for future in as_completed(futures):
result = future.result()
print(result)異步編程
concurrent.futures與asyncio結(jié)合使用
concurrent.futures可以與asyncio一同使用,實(shí)現(xiàn)異步編程的優(yōu)勢(shì)。
import asyncio
from concurrent.futures import ThreadPoolExecutor
async def main():
loop = asyncio.get_event_loop()
with ThreadPoolExecutor() as executor:
result = await loop.run_in_executor(executor, some_blocking_function, args)
print(result)
asyncio.run(main())錯(cuò)誤處理和超時(shí)
concurrent.futures提供了處理錯(cuò)誤和設(shè)置超時(shí)的機(jī)制,確保程序在執(zhí)行過(guò)程中具有魯棒性。
from concurrent.futures import ThreadPoolExecutor, TimeoutError
def some_function():
# 一些可能引發(fā)異常的操作
with ThreadPoolExecutor() as executor:
future = executor.submit(some_function)
try:
result = future.result(timeout=1)
except TimeoutError:
print("任務(wù)超時(shí)")
except Exception as e:
print(f"發(fā)生錯(cuò)誤: {e}")實(shí)際應(yīng)用
數(shù)據(jù)并行處理
使用ProcessPoolExecutor并行處理大規(guī)模數(shù)據(jù)集,提高處理速度。
from concurrent.futures import ProcessPoolExecutor
data = get_large_dataset()
with ProcessPoolExecutor() as executor:
results = executor.map(process_data, data)異步爬蟲(chóng)
結(jié)合concurrent.futures和asyncio,實(shí)現(xiàn)高效的異步爬蟲(chóng)。
import asyncio
from concurrent.futures import ThreadPoolExecutor
async def fetch(url):
# 異步請(qǐng)求數(shù)據(jù)
async def main():
loop = asyncio.get_event_loop()
with ThreadPoolExecutor() as executor:
tasks = [loop.run_in_executor(executor, fetch, url) for url in urls]
await asyncio.gather(*tasks)
asyncio.run(main())總結(jié)
concurrent.futures為Python開(kāi)發(fā)者提供了強(qiáng)大的并行化編程工具,通過(guò)ThreadPoolExecutor和ProcessPoolExecutor,可以輕松實(shí)現(xiàn)多線程和多進(jìn)程的任務(wù)并行執(zhí)行。同時(shí),結(jié)合asyncio實(shí)現(xiàn)異步編程,加速程序的執(zhí)行。在實(shí)際應(yīng)用中,可以通過(guò)map方法、submit方法、as_completed函數(shù)等方式,高效地處理大規(guī)模數(shù)據(jù)和異步任務(wù)。通過(guò)深入理解和靈活運(yùn)用concurrent.futures,開(kāi)發(fā)者能夠更好地優(yōu)化程序性能,提高代碼的可維護(hù)性。
以上就是Python Concurrent Futures解鎖并行化編程的魔法示例的詳細(xì)內(nèi)容,更多關(guān)于Python Concurrent Futures并行化編程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式)
這篇文章主要介紹了使用keras實(shí)現(xiàn)非線性回歸(兩種加激活函數(shù)的方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python datetime 格式化 明天,昨天實(shí)例
這篇文章主要介紹了Python datetime 格式化 明天,昨天實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
python對(duì)批量WAV音頻進(jìn)行等長(zhǎng)分割的方法實(shí)現(xiàn)
這篇文章主要介紹了python對(duì)批量WAV音頻進(jìn)行等長(zhǎng)分割的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python實(shí)現(xiàn)七大查找算法的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)七大查找算法的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
python實(shí)現(xiàn)的多線程端口掃描功能示例
這篇文章主要介紹了python實(shí)現(xiàn)的多線程端口掃描功能,結(jié)合實(shí)例形式分析了Python基于socket的端口掃描具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
python?教程之blinker?信號(hào)庫(kù)
這篇文章主要介紹了python?教程之blinker?信號(hào)庫(kù),文章基于python的相關(guān)資料展開(kāi)詳細(xì)的內(nèi)容說(shuō)明。具有一定的參考價(jià)價(jià)值,需要的小伙伴可以參考一下2022-05-05
python使用多線程查詢數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例
這篇文章主要介紹了python使用多線程查詢數(shù)據(jù)庫(kù)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

