Python?Concurrent?Futures解鎖并行化編程的魔法示例
基礎概念
ThreadPoolExecutor和ProcessPoolExecutor
concurrent.futures提供了兩個主要的執(zhí)行器:ThreadPoolExecutor和ProcessPoolExecutor。前者在單個進程中使用多線程執(zhí)行任務,而后者則在多個進程中執(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對象
Future是異步計算的結果的占位符,表示一個可能在未來完成的操作。通過submit方法提交任務后,會返回一個Future對象,可以通過它獲取任務的狀態(tài)和結果。
from concurrent.futures import ThreadPoolExecutor
def some_function(data):
# 一些耗時的操作
return result
with ThreadPoolExecutor() as executor:
future = executor.submit(some_function, data)
result = future.result()并行化任務執(zhí)行
map方法
Executor對象的map方法可以方便地并行執(zhí)行函數(shù),并返回結果。
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方法可以異步地提交任務,而as_completed函數(shù)可以按完成順序迭代Future對象。
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結合使用
concurrent.futures可以與asyncio一同使用,實現(xiàn)異步編程的優(yōu)勢。
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())錯誤處理和超時
concurrent.futures提供了處理錯誤和設置超時的機制,確保程序在執(zhí)行過程中具有魯棒性。
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("任務超時")
except Exception as e:
print(f"發(fā)生錯誤: {e}")實際應用
數(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)異步爬蟲
結合concurrent.futures和asyncio,實現(xiàn)高效的異步爬蟲。
import asyncio
from concurrent.futures import ThreadPoolExecutor
async def fetch(url):
# 異步請求數(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())總結
concurrent.futures為Python開發(fā)者提供了強大的并行化編程工具,通過ThreadPoolExecutor和ProcessPoolExecutor,可以輕松實現(xiàn)多線程和多進程的任務并行執(zhí)行。同時,結合asyncio實現(xiàn)異步編程,加速程序的執(zhí)行。在實際應用中,可以通過map方法、submit方法、as_completed函數(shù)等方式,高效地處理大規(guī)模數(shù)據(jù)和異步任務。通過深入理解和靈活運用concurrent.futures,開發(fā)者能夠更好地優(yōu)化程序性能,提高代碼的可維護性。
以上就是Python Concurrent Futures解鎖并行化編程的魔法示例的詳細內(nèi)容,更多關于Python Concurrent Futures并行化編程的資料請關注腳本之家其它相關文章!
相關文章
已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch
這篇文章主要給大家介紹了關于已安裝Pytorch卻提示no?moudle?named?'torch'(沒有名稱為torch的模塊)的相關資料,當提示"No module named 'torch'"時,可能是由于安裝的Pytorch版本與當前環(huán)境不匹配導致的,需要的朋友可以參考下2023-11-11
PyCharm接入DeepSeek實現(xiàn)AI編程的操作流程
DeepSeek 是一家專注于人工智能技術研發(fā)的公司,致力于開發(fā)高性能、低成本的 AI 模型,接下來,我們把DeepSeek接入到PyCharm中,并利用其能力輔助我們進行代碼開發(fā),感興趣的小伙伴跟著小編一起來看看吧2025-01-01
Python永久配置國內(nèi)鏡像源安裝再也不用擔心卡頓
這篇文章主要為大家介紹了Python如何永久配置國內(nèi)鏡像源,從此安裝再也不用擔心卡頓,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

