我需要一些幫助來控制子行程。我真的不確定我也需要做/研究哪個方向的代碼。
目前,我正在使用 python 控制軟體的運行,如下面的代碼所示。
import subprocess
# Declaring some input and output names for the third-party program
label = ['fname1', 'fname2', 'fname3', 'fname4']
input_list = [ 'inp.' l for l in label ]
output_list = [ 'out.' l for l in label ]
# Run all sets of input and output using the third-party software
for in, out in zip(input_list, output_list):
#The bash command to run the executable
command = f"mpirun pw.x < {in} > {out}"
subprocess.run(command, shell=True)
我的計算機有 64 個邏輯核心,正如我用軟體測驗過的那樣,使用 32 和 64 不會改變計算速度,因此,我想編輯代碼以容納兩個并發的 subprocess.run 和mpirun -n 32 ....
我不知道如何做并發的東西,比如排隊和控制在給定時間允許運行多少個子行程實體。
請問哪個模塊/庫可以幫助我完成這項作業?當然,我們將非常感謝代碼示例。
PS PBS/SLURM 系統不是一個選項,因為我還在 python 腳本中做一些處理作業。
謝謝!
uj5u.com熱心網友回復:
如果你想mpirun -n 32并行運行兩次,試試這個:
import concurrent.futures
labels = ['fname1', 'fname2']
with concurrent.futures.ProcessPoolExecutor(max_workers=len(labels)) as pool:
for label in labels:
command = f"mpirun -n 32 pw.x < inp.{label} > out.{label}"
pool.submit(subprocess.run, command, shell=True)
該with陳述句將在最后關閉池,使其等待所有作業完成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/434633.html
標籤:python-3.x 重击 子进程
