1. 程式人生 > >Python多進程VS多線程

Python多進程VS多線程

sub 多進程 調用 sort product tools spa blog 必須

多線程:適用於處理I/O密集型任務和並發執行的阻塞操作
多進程:適用於處理計算密集型任務

# 多進程
import itertools
from concurrent.futures import ProcessPoolExecutor
result = []

# 回調函數,通過add_done_callback任務完成後調用
def when_done(r):
    # when_done在主進程中運行
    result.append(r.result())

"""
    with class_a() as a: 上下文管理器
"""
with ProcessPoolExecutor() as pool:
    
for keep_stock_threshold, buy_change_threshold in itertools.product(keep_stock_list, buy_change_list): """ submit提交任務:使用calc函數和的參數通過submit提交到獨立進程 提交的任務必須是簡單函數,進程並行不支持類方法、閉包等 函數參數和返回值必須兼容pickle序列化,進程間的通信需要 """ future_result
= pool.submit(calc, keep_stock_threshold, buy_change_threshold) # 當進程完成任務即calc運行結束後的回調函數 future_result.add_done_callback(when_done) print(sorted(result)[::-1][:10])

# 多線程
from concurrent.futures import ThreadPoolExecutor

result = []
def when_done(r): result.append(r.result()) with ThreadPoolExecutor(max_workers=8) as pool: for keep_stock_threshold, buy_change_threshold in itertools.product(keep_stock_list, buy_change_list): future_result = pool.submit(calc, keep_stock_threshold, buy_change_threshold) future_result.add_done_callback(when_done)

Python多進程VS多線程