1

编辑:我已经更新了我的问题和代码。

我正在从批处理文件运行 python 脚本(在数百个文件上)。问题是,某些文件上的 python 脚本需要很长时间才能运行。我想要的是我的 python 脚本不应该在单个文件上运行超过 5 分钟。如果需要超过 5 分钟,我只想杀死我正在运行的脚本并移动到下一个文件,如下面的批处理文件代码中所写。

我想要的是,将 300 秒定义为我的代码可以在一个文件上花费的最长时间。如果它需要超过 300 秒,批处理文件应该杀死它。但是,当时间少于 300 秒时,它不应等待 300 秒,而应立即移至下一条语句。

@echo off
setlocal enableextensions disabledelayedexpansion

echo FILE NO: 1
rem filename1 is not a variable name but the name of the file itself
start "Python" python "Code.py" filename1 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename1 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 2
start "Python" python "Code.py" filename2 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename2 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 3
start "Python" python "Code.py" filename3 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename3 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 4
start "Python" python "Code.py" filename4 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename4 var2 & call :timeoutProcess "python.exe" 300
echo FILE NO: 5
start "Python" python "Code.py" filename5 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename5 var2 & call :timeoutProcess "python.exe" 300
.
.
.
.
.
.
.
echo FILE NO: 200
start "Python" python "Code.py" filename200 var1 & call :timeoutProcess "python.exe" 300
start "Python" python "Code.py" filename200 var2 & call :timeoutProcess "python.exe" 300

exit /b

:timeoutProcess process timeout [leave]
rem process = name of process to monitor
rem timeout = timeout in seconds to wait for process to end
rem leave   = 1 if process should not be killed on timeout
for /l %%t in (1 1 %~2) do ( 
    timeout /t 1 >nul
    tasklist | find /i "%~1" >nul || exit /b 0
)
if not "%~3"=="1" taskkill /f /im "%~1" >nul
exit /b 1

如果我将它运行到 filename5,这段代码就可以正常工作。但是,当我将它运行到 filename200 时,此代码仅针对 filename1 运行,并且直接移动到 filename13,然后是 filename170。我无法理解这个问题。似乎没有任何语法错误。

我是编写批处理文件的新手。请帮助解决这个问题。如果您有更好的选择来解决这个问题(而不是超时),那么请告诉我。

PS:我也尝试在 Python 中从 Eventlet 包中使用 Timeout 函数,但是这个函数在我的文件上不能正常工作,因此我认为我应该尝试从批处理文件中超时。我正在使用 Windows 机器并使用 Python 2.7。

4

1 回答 1

1

也许最简单的方法是循环检查进程是否已经结束。如果该过程结束,则离开循环。如果循环结束,终止进程

@echo off
    setlocal enableextensions disabledelayedexpansion

    start "" python "E:\Code1.py"
    call :timeoutProcess "python.exe" 300

    start "" python "E:\Code2.py"
    call :timeoutProcess "python.exe" 300

    exit /b

:timeoutProcess process timeout [leave]
    rem process = name of process to monitor
    rem timeout = timeout in seconds to wait for process to end
    rem leave   = 1 if process should not be killed on timeout
    for /l %%t in (1 1 %~2) do (
        timeout /t 1 >nul
        tasklist | find /i "%~1" >nul || exit /b 0
    )
    if not "%~3"=="1" taskkill /f /im "%~1" >nul 2>nul
    if %errorlevel% equ 128 ( exit /b 0 ) else ( exit /b 1 )

(代码改编自先前的答案

于 2014-09-16T07:50:34.243 回答