標籤:end ted worker res 簡單函數 adp div c函數 使用
多線程:適用於處理I/O密集型任務和並發執行的阻塞操作
多進程:適用於處理計算密集型任務
# 多進程import itertoolsfrom concurrent.futures import ProcessPoolExecutorresult = []# 回呼函數,通過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 ThreadPoolExecutorresult = []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多線程