python多线程加速for循环

#求帮助#下面的这段代码,针对for循环,当I和D均有上百个值时,如何用多线程或者其他手段大大提高代码的执行效率?

#从两个excel中读取ID序列,以“测试I-D”组合命名被复制的文件夹,修改txt文本第a行b列封装函数,批量依次运行exe程序
import pandas as pd# 读取 Excel 文件
import os
dI = pd.read_excel('F:\测试\I.xlsx')
dD = pd.read_excel('F:\测试\D.xlsx')
# 获取某一列的数据并转化为列表-定义变量I和D的取值列表
I_list = dI['强度'].tolist()
D_list = dD['时间'].tolist()
print(I_list)
print(D_list)
def modify_txt_file(file_path, a, b, value):
            with open(file_path, "r+") as f:
                lines = f.readlines()
                line = lines[a-1].split()
                line[b-1] = str(value)
                lines[a-1] = " ".join(line) + "\n"
                f.seek(0)
                f.writelines(lines)
                f.truncate() 
# 遍历I和D的取值组合,生成多个文件夹并复制文件
for I in I_list:
    for D in D_list:
        folder_name = f"测试{I}-{D}"
        os.makedirs(os.path.join("F:\\测试\\测试文件", folder_name))
        os.system(f"xcopy /E /I \"F:\\测试\\模型模板10m\" \"F:\\测试\\测试文件\\{folder_name}\"")
         # 修改input.txt文件内容
        input_file = os.path.join("F:\测试\测试文件\\", folder_name, "input_in.txt")
        modify_txt_file(input_file, 6, 5, D * 3600)
        modify_txt_file(input_file, 24, 2, D * 3600)
        modify_txt_file(input_file, 70, 1, D * 3600)
        line10 = f"{0.001 * I / 86400:.2e}"
        modify_txt_file(input_file, 22, 1, line10)
        
        # 运行A.exe文件
        os.chdir(os.path.join("F:\\测试\\测试文件\\", folder_name))
        topoindex_file = os.path.join("F:\\测试\\测试文件\\", folder_name, "A.exe")
        os.system(topoindex_file)
 
# 运行B.exe文件
for I in I_list:
    for D in D_list:
        folder_name = f"测试{I}-{D}"
        os.chdir(os.path.join("F:\\测试\\测试文件\\", folder_name))
        trigrs_file = os.path.join("F:\\测试\\测试文件\\", folder_name, "B.exe")
        os.system(trigrs_file)
 
 


import  pandas  as  pd
import  os
from  multiprocessing  import  Pool

#  读取  Excel  文件
dI  =  pd.read_excel('F:\测试\I.xlsx')
dD  =  pd.read_excel('F:\测试\D.xlsx')

#  获取某一列的数据并转化为列表-定义变量I和D的取值列表
I_list  =  dI['强度'].tolist()
D_list  =  dD['时间'].tolist()
print(I_list)
print(D_list)

#  定义修改txt文件的函数
def  modify_txt_file(file_path,  a,  b,  value):
        with  open(file_path,  "r+")  as  f:
                lines  =  f.readlines()
                line  =  lines[a-1].split()
                line[b-1]  =  str(value)
                lines[a-1]  =  "  ".join(line)  +  "\n"
                f.seek(0)
                f.writelines(lines)
                f.truncate()  

#  定义运行A.exe文件的函数
def  run_A_exe(folder_name,  D):
        input_file  =  os.path.join("F:\测试\测试文件\\",  folder_name,  "input_in.txt")
        modify_txt_file(input_file,  6,  5,  D  *  3600)
        modify_txt_file(input_file,  24,  2,  D  *  3600)
        modify_txt_file(input_file,  70,  1,  D  *  3600)
        line10  =  f"{0.001  *  I  /  86400:.2e}"
        modify_txt_file(input_file,  22,  1,  line10)
        os.chdir(os.path.join("F:\\测试\\测试文件\\",  folder_name))
        topoindex_file  =  os.path.join("F:\\测试\\测试文件\\",  folder_name,  "A.exe")
        os.system(topoindex_file)

#  定义运行B.exe文件的函数
def  run_B_exe(folder_name):
        os.chdir(os.path.join("F:\\测试\\测试文件\\",  folder_name))
        trigrs_file  =  os.path.join("F:\\测试\\测试文件\\",  folder_name,  "B.exe")
        os.system(trigrs_file)

#  定义处理单个参数组合的函数
def  process_parameters(I,  D):
        folder_name  =  f"测试{I}-{D}"
        os.makedirs(os.path.join("F:\\测试\\测试文件",  folder_name))
        os.system(f"xcopy  /E  /I  \"F:\\测试\\模型模板10m\"  \"F:\\测试\\测试文件\\{folder_name}\"")
        run_A_exe(folder_name,  D)
        run_B_exe(folder_name)

#  定义启动多进程的函数
def  run_multiprocess():
        with  Pool(processes=4)  as  pool:
                parameter_list  =  [(I,  D)  for  I  in  I_list  for  D  in  D_list]
                pool.starmap(process_parameters,  parameter_list)

if  __name__  ==  '__main__':
        run_multiprocess()

如有帮助,请采纳,谢谢

可以用线程池,把任务丢进线程池里执行