# -*- coding: utf-8 -*- # @Date: 2017-08-26 # @Original: # 多進程 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)