我正在尝试编写一个Python脚本来启动有限元模拟代码的许多子进程。我已经能够让Python生成所有子进程,但我的问题是进程池限制似乎被忽略了。如果我尝试生成九个进程但设置pool = mp.Pool(processes=2)
,则所有九个进程仍然会立即启动。如果进程数超过LSDYNA许可证数或计算机上的处理器数,则会出现问题。我已经阅读了很多关于多处理程序包的内容,但这仍然是我第一次尝试使用它。我在这里做错了吗?
import os
import multiprocessing as mp
import subprocess
import glob
import platform
def run_LSDYNA(individual_sim_dir, model_name, solver_path):
# set LSDYNA environment variables
os.environ['lstc_license'] = 'network'
os.environ['lstc_license_server'] = 'xxx.xx.xx.xx'
# call the cmd prompt with the LSDYNA command line
if platform.system() == 'Windows':
subprocess.call('start "" /D "%s" "%s" i=%s ncpu=1 memory=100m' % (individual_sim_dir, solver_path,
model_name), shell=True)
# elif platform.system() == 'Linux':
# elif platform.system() == 'Darwin':
def unpack(args):
# unpacks each set of arguments into individuals and sends them to the run_LSDYNA function
run_LSDYNA(*args)
def parallel(sim_dir, procs, model_name, solver_path):
# limit the number of parallel processes to the specified value
pool = mp.Pool(processes=procs)
# Loop through the simulation directories and add the arguments for each to the list
args = []
for individual_sim_dir in glob.glob(os.path.join(sim_dir, 'channel_*')):
args.append((individual_sim_dir, model_name, solver_path))
# Send the list of arguments to the unpack function
pool.map_async(unpack, args)
pool.close()
pool.join()
if __name__ == "__main__":
mp.freeze_support()
parallel('C:\Users\me\Desktop\script_test\Lower_Leg_Sims', 2, 'symmetry_with_boot_1_90_180.k', "C:\LSDYNA\program\ls971_s_R5.1.1_winx64_p.exe")