windows 可以在cmd中启动多线程命令吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4837369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:05:44  来源:igfitidea点击:

Possible to launch multiple threads of commands in cmd?

windowscmdmultiprocessing

提问by Chirag

I have about 290 files that I need to optimize in a short period of time.

我有大约 290 个文件需要在短时间内优化。

When I do optipng *.pngit takes about 10 minutes to complete the transaction.

当我这样做optipng *.png时,大约需要 10 分钟才能完成交易。

However when I do optipng a*.pngand optipng m*.pngin two separate command line it gets the work done in 5 minutes.

然而,当我这样做optipng a*.png,并optipng m*.png在两个单独的命令行它会在5分钟内完成的工作。

Now is there a way I can launch about 20 processes at the same time which will get the work done faster and not take up all of the space on my Desktop?

现在有没有一种方法可以同时启动大约 20 个进程,这样可以更快地完成工作并且不占用桌面上的所有空间?

回答by Joey

I have written a batch file that executes only a maximum number of commands a while ago: Parallel execution of shell processes:

我刚才写了一个批处理文件,它只执行最大数量的命令:shell 进程的并行执行

@echo off
for /l %%i in (1,1,20) do call :loop %%i
goto :eof

:loop
call :checkinstances
if %INSTANCES% LSS 5 (
    rem just a dummy program that waits instead of doing useful stuff
    rem but suffices for now
    echo Starting processing instance for %1
    start /min wait.exe 5 sec
    goto :eof
)
rem wait a second, can be adjusted with -w (-n 2 because the first ping returns immediately;
rem otherwise just use an address that's unused and -n 1)
echo Waiting for instances to close ...
ping -n 2 ::1 >nul 2>&1
rem jump back to see whether we can spawn a new process now
goto loop
goto :eof

:checkinstances
rem this could probably be done better. But INSTANCES should contain the number of running instances afterwards.
for /f "usebackq" %%t in (`tasklist /fo csv /fi "imagename eq wait.exe"^|wc -l`) do set INSTANCES=%%t
goto :eof

It spawns a maximum of four new processes that execute in parallel and minimized. Wait time needs to be adjusted probably, depending on how much each process does and how long it is running. You probably also need to adjust the process name for which tasklist is looking if you're doing something else.

There is no way to properly count the processes that are spawned by this batch, though. One way would be to create a random number at the start of the batch (%RANDOM%) and create a helper batch that does the processing (or spawns the processing program) but which can set its window title to a parameter:

@echo off
title %1
"%2" "%3"

This would be a simple batch that sets its title to the first parameter and then runs the second parameter with the third as argument. You can then filter in tasklist by selecting only processes with the specified window title (tasklist /fi "windowtitle eq ..."). This should work fairly reliable and prevents too many false positives. Searching for cmd.exewould be a bad idea if you still have some instances running, as that limits your pool of worker processes.

You can use %NUMBER_OF_PROCESSORS%to create a sensible default of how many instances to spawn.

You can also easily adapt this to use psexecto spawn the processes remotely (but wouldn't be very viable as you have to have admin privileges on the other machine as well as provide the password in the batch). You would have to use process names for filtering then, though.

@echo off
for /l %%i in (1,1,20) do call :loop %%i
goto :eof

:loop
call :checkinstances
if %INSTANCES% LSS 5 (
    rem just a dummy program that waits instead of doing useful stuff
    rem but suffices for now
    echo Starting processing instance for %1
    start /min wait.exe 5 sec
    goto :eof
)
rem wait a second, can be adjusted with -w (-n 2 because the first ping returns immediately;
rem otherwise just use an address that's unused and -n 1)
echo Waiting for instances to close ...
ping -n 2 ::1 >nul 2>&1
rem jump back to see whether we can spawn a new process now
goto loop
goto :eof

:checkinstances
rem this could probably be done better. But INSTANCES should contain the number of running instances afterwards.
for /f "usebackq" %%t in (`tasklist /fo csv /fi "imagename eq wait.exe"^|wc -l`) do set INSTANCES=%%t
goto :eof

它最多产生四个并行执行并最小化的新进程。等待时间可能需要调整,这取决于每个进程做了多少以及它运行了多长时间。如果您正在做其他事情,您可能还需要调整任务列表正在查找的进程名称。

但是,无法正确计算此批处理产生的进程。一种方法是在批处理开始时创建一个随机数 ( %RANDOM%) 并创建一个辅助批处理来执行处理(或生成处理程序),但可以将其窗口标题设置为参数:

@echo off
title %1
"%2" "%3"

这将是一个简单的批处理,将其标题设置为第一个参数,然后使用第三个参数运行第二个参数。然后,您可以通过仅选择具有指定窗口标题 ( tasklist /fi "windowtitle eq ...") 的进程在任务列表中进行过滤。这应该工作相当可靠,并防止太多误报。cmd.exe如果您仍有一些实例在运行,搜索将是一个坏主意,因为这会限制您的工作进程池。

您可以使用%NUMBER_OF_PROCESSORS%来创建一个合理的默认值来生成多少个实例。

您还可以轻松地调整它以用于psexec远程生成进程(但不太可行,因为您必须在另一台机器上具有管理员权限并在批处理中提供密码)。但是,您将不得不使用进程名称进行过滤。

回答by Arseny

Looks like you can write a batch file and run your commands asynchronously from that file.

看起来您可以编写一个批处理文件并从该文件异步运行您的命令。

Running Windows batch file commands asynchronously

异步运行 Windows 批处理文件命令