我有一个Windows批处理文件,它需要对多个文件执行相同的操作。有没有一种方法可以让计算机使用不同的处理器核心来加速处理过程?否则,由于相同的处理器一直在使用,处理时间会更长。
我有一个Windows批处理文件,它需要对多个文件执行相同的操作。有没有一种方法可以让计算机使用不同的处理器核心来加速处理过程?否则,由于相同的处理器一直在使用,处理时间会更长。
start /b
来进行多线程处理。@echo off
setlocal EnableDelayedExpansion
set "file1=%~1"
:startExtraThread
if NOT "%~2"=="" (
start "Thread for %~2" /b "cmd /c %~f0 %~2"
shift
goto :startExtraThread
)
do my job with file1
@echo off
setlocal EnableDelayedExpansion
rem Multi-thread scheme in Batch files
rem Antonio Perez Ayala aka Aacini
set "myID=%~2"
if "%~1" neq "" goto %1
set "signal=X"
for /L %%i in (1,1,10) do set "signal=!signal!!signal!"
rem Create the list of start commands for the concurrent working threads
set "numThreads=%NUMBER_OF_PROCESSORS%"
set "threads="
del availableThread* 2> NUL
for /L %%i in (1,1,%numThreads%) do (
set "threads=!threads! & start "" /B cmd /C "%~NX0" Thread %%i"
> availableThread%%i echo %%i
)
echo Start of application
del requestForThread* 2> NUL
( %threads:~3% ) | "%~NX0" Main
echo End of application
goto :EOF
:Main
echo Main - started
rem Controlling code: process all files and assign each one to available threads
for /F "delims=" %%f in ('dir /A-D /B *.txt') do (
rem Get the number of next available thread; waits for anyone, if necessary
if not exist availableThread* (
echo X > mainIsWaiting
echo Main - waiting for an available thread
set /P availableSignal=
del mainIsWaiting
)
set "nextThread="
for %%t in (availableThread*) do if not defined nextThread (
set /P "nextThread=" < %%t
del %%t
)
if not defined nextThread (
echo SYNCHRO FAILED^^!
goto waitThreadsEnds
)
rem Assign the next file to the available thread
echo %%f> requestForThread!nextThread!
echo Main - file "%%f" assigned to thread #!nextThread!
)
echo Main - all files sent to processing
rem Wait until all threads ends their tasks
:waitThreadsEnds
set availableThreads=0
for %%t in (availableThread*) do set /A availableThreads+=1
if %availableThreads% lss %numThreads% goto :waitThreadsEnds
del availableThread*
rem Send "exit" signals to all threads
for /L %%i in (1,1,%numThreads%) do echo exit> requestForThread%%i
echo Main - end
goto :EOF
:Thread
rem Wait until a request for this thread appear
if not exist requestForThread%myID% goto Thread
set "request="
set /P "request=" < requestForThread%myID%
if not defined request goto Thread
del requestForThread%myID%
if "%request%" equ "exit" goto :EOF
rem Process the file
echo %time% - Thread #%myID% start: "%request%" > CON
set /A seconds=6+%random:~-1%
ping localhost -n %seconds% > NUL
echo %time% - Thread #%myID% end: "%request%" > CON
rem Inform that this thread is available for other task
> availableThread%myID% echo %myID%
if exist mainIsWaiting echo %signal:~0,1021%
goto Thread