[英]Python - how to use multiprocessing with class instance
currently I run a loop in a loop in a loop that then pass a new set of parameters of an already instanciated class and then call at first a class.reset() function and then the class.main() function.
由於 class.run 的 CPU 強度很高,我確實想多處理它,但我沒有找到如何執行此操作的示例。
下面是需要多處理的代碼:
st = Strategy() /// Strategy is my class
for start_delay in range(0, PAR_BT_CYCLE_LENGTH_END, 1):
for cycle_length in range(PAR_BT_CYCLE_LENGTH_START, PAR_BT_CYCLE_LENGTH_END+1, 1):
for cycle_pos in range(PAR_BT_N_POS_START, PAR_BT_N_POS_END+1, 1):
st.set_params(PAR_BT_START_CAPITAL, start_delay, cycle_length, cycle_pos, sBT,
iPAR_BT_TF1, iPAR_BT_TF2, iPAR_BT_TF3, iPAR_BT_TF4,
iPAR_BT_TFW1, iPAR_BT_TFW2, iPAR_BT_TFW3, iPAR_BT_TFW4)
st.reset()
bt = st.main()
# do something with return values (list) in bt
# after all processes have finished - use return values of all processes
讓它作為多個進程工作的最佳方法是什么?
您可以使用concurrent.futures
中的ProcessPoolExecutor
。
from concurrent.futures import ProcessPoolExecutor, as_completed
def run_strategy(*args):
st = Strategy(
st.set_params(*args)
st.reset()
bt = st.main()
return bt
ex = ProcessPoolExecutor()
futures = []
for start_delay in range(0, PAR_BT_CYCLE_LENGTH_END, 1):
for cycle_length in range(PAR_BT_CYCLE_LENGTH_START, PAR_BT_CYCLE_LENGTH_END+1, 1):
for cycle_pos in range(PAR_BT_N_POS_START, PAR_BT_N_POS_END+1, 1):
args = (
PAR_BT_START_CAPITAL,
start_delay,
cycle_length,
cycle_pos,
sBT,
iPAR_BT_TF1,
iPAR_BT_TF2,
iPAR_BT_TF3,
iPAR_BT_TF4,
iPAR_BT_TFW1,
iPAR_BT_TFW2,
iPAR_BT_TFW3,
iPAR_BT_TFW4
)
ex.submit(run_strategy, *args)
# collect the returned bts
bt_results = []
for f in as_completed(futures):
bt_results.append(f.result())
ex.shutdown()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.