有没有办法在批处理文件中完成多线程或并行处理?

Is there a way to accomplish multithreading or parallel processes in a batch file?

所以我有一个批处理文件,它在给定一些输入参数的情况下执行模拟,然后通过 awk、R 和 Python 处理输出数据。目前,输入参数通过一些嵌套的for循环传递到模拟中,模拟的每次迭代都会运行一个接一个。我希望并行执行模拟,因为目前有 1,000 多个案例,所以在我看来,我可以让核心 1 处理 sims 1-250,核心 2 处理 sims 251-500,等等

本质上我想做的是:

  1. 运行 跨多核模拟的每个案例
  2. 每次模拟完成后,开始输出数据处理

我试过使用 start /affinity n simulation.exe,但这里的问题是所有模拟将同时执行,所以当它到达 post 处理调用时,它会出错,因为数据还没有生成。有 start /w 命令,但我不确定这是否改进了模拟。我想到的一个想法是在每次模拟完成后更新一个变量,然后仅在变量达到 n 运行s.

后才开始 post 处理

以下是我现在正在做的事情的摘录:

    for %%f in (1 2 3) do (
            for %%a in (4 5 6) do (
                for %%b in (7 8 9) do (
                    call :mission %%f %%a %%b
                )
            )
         )
    some gawk scripts
    some python scripts
    some r scripts
    go to :exit

:mission
   sed -e 's/text1/%1/' -e 's/text2/%2/' -e 's/text3/%3/'
   simulation.exe
   go to :exit

:exit

这是我用来测试一些并行处理的东西:

start /affinity 1 C:\Users4890\R-4.1.1\bin\Rscript.exe test1.R
start /affinity 2 C:\Users4890\R-4.1.1\bin\Rscript.exe test2.R
start /affinity 3 C:\Users4890\R-4.1.1\bin\Rscript.exe test3.R
start /affinity 4 C:\Users4890\R-4.1.1\bin\Rscript.exe test4.R

C:\Users4890\R-4.1.1\bin\Rscript.exe plotting.R

我实际上能够通过执行以下操作来完成此操作:

setlocal
set "lock=%temp%\wait%random%.lock"

:: Launch processes asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends
start /affinity 1 9>"%lock%1" Rscript test1.R
start /affinity 2 9>"%lock%2" Rscript test2.R
start /affinity 4 9>"%lock%3" Rscript test3.R
start /affinity 8 9>"%lock%4" Rscript test4.R

:Wait for all processes to finish
1>nul 2>nul ping /n 2 ::1
for %%F in ("%lock%*") do (
 (call ) 9>"%%F" || goto :Wait
) 2>nul

del "%lock%*"

Rscript plotting.R