【发布时间】:2019-07-23 19:03:13
【问题描述】:
我在编写批处理文件方面的经验为零,实际上我指的是较早的帖子 “Waiting for parallel batch scripts”,以及dbenham的回答。
@echo off
setlocal
set "lock=%temp%\wait%random%.lock"
:: Launch one and two asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" one.bat
start "" cmd /c 9>"%lock%2" two.bat
:Wait for both scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2) do (
( rem
) 9>"%lock%%%N" || goto :Wait
) 2>nul
::delete the lock files
del "%lock%*"
:: Launch three and four asynchronously
start "" cmd /c three.bat
start "" cmd /c four.bat
他的回答很好,但我想调整他的解决方案,而不是同时运行 2 个文件,而是 8 个,然后再运行 8 个,依此类推......
有人可以帮我吗?
到目前为止我尝试的是这个(3 个块,每个块 8 个批处理文件)
@echo off
setlocal
set "lock=%temp%\wait%random%.lock"
:: Launch 8 files asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" one.bat
start "" cmd /c 9>"%lock%2" two.bat
start "" cmd /c 9>"%lock%3" three.bat
start "" cmd /c 9>"%lock%4" four.bat
start "" cmd /c 9>"%lock%5" five.bat
start "" cmd /c 9>"%lock%6" six.bat
start "" cmd /c 9>"%lock%7" seven.bat
start "" cmd /c 9>"%lock%8" eight.bat
:Wait for all scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2 3 4 5 6 7 8) do (
( rem
) 9>"%lock%%%N" || goto :Wait
) 2>nul
::delete the lock files
del "%lock%*"
:: Launch 8 files asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" nine.bat
start "" cmd /c 9>"%lock%2" ten.bat
start "" cmd /c 9>"%lock%3" eleven.bat
start "" cmd /c 9>"%lock%4" twelve.bat
start "" cmd /c 9>"%lock%5" thirteen.bat
start "" cmd /c 9>"%lock%6" fourteen.bat
start "" cmd /c 9>"%lock%7" fifteen.bat
start "" cmd /c 9>"%lock%8" sixteen.bat
:Wait for all scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2 3 4 5 6 7 8) do (
( rem
) 9>"%lock%%%N" || goto :Wait
) 2>nul
::delete the lock files
del "%lock%*"
:: Launch three and four asynchronously
start "" cmd /c seventeen.bat
start "" cmd /c eighteen.bat
start "" cmd /c nineteen.bat
start "" cmd /c twenty.bat
start "" cmd /c twenty-one.bat
start "" cmd /c twenty-two.bat
start "" cmd /c twenty-three.bat
start "" cmd /c twenty-four.bat
但它似乎无法正常工作。 Normaly 一个包含八个批处理文件的块将在最大之后完成。 3 小时,但我等了将近 24 小时,他似乎被困在第一个街区......
关键信息:当一个批处理文件(或作业)被执行时,一个“cmd”窗口会打开大约 5 秒钟,然后关闭并启动另一个程序,该程序正在执行 max 的实际计算工作. 3小时。我查看了任务管理器,结果是,启动了 3 个进程。 20 秒后的“standard.exe”和“eliT_DriverLM.exe”,5 秒后的“python.exe”。由于当这 3 个进程不再运行时(不再显示在任务管理器中),该进程就完成了,程序应该至少等待“standard.exe”终止。
【问题讨论】:
-
那么,到目前为止,您为实现目标做了哪些尝试?请edit您的问题并提供minimal reproducible example您的尝试!
-
有两个异步调用
start "" cmd /c 9>"%lock%1" one.bat和start "" cmd /c 9>"%lock%2" two.bat;只需编写 8 行此语法模式即可。此外,还有一个等到两个锁定文件不再锁定的代码sn-p。要等到 八个 锁定文件不再被锁定,请使用for %%N in (1 2 3 4 5 6 7 8) do (... -
Related 和 possible duplicate 使用另一种方法。
标签: batch-file locking