Python多处理池limi的问题

2024-04-20 03:02:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个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")

Tags: thepathnameimportmodel进程osdir