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

发布于 2024-10-14 18:56:16 字数 252 浏览 1 评论 0原文

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

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

但是,当我在两个单独的命令行中执行 optipng a*.pngoptipng m*.png 时,它可以在 5 分钟内完成工作。

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱了心跳 2024-10-21 18:56:16

我不久前编写了一个仅执行最大数量命令的批处理文件: shell进程的并行执行

<前><代码>@echo 关闭
for /l %%i in (1,1,20) do call :loop %%i
转到:eof

:环形
调用:检查实例
如果 %INSTANCES% LSS 5 (
rem 只是一个等待而不做有用的事情的虚拟程序
rem 但现在就足够了
echo 正在启动 %1 的处理实例
启动 /min wait.exe 5 秒
转到:eof

rem 稍等一下,可以用 -w 调整(-n 2 因为第一个 ping 立即返回;
rem 否则只使用未使用的地址和 -n 1)
echo 正在等待实例关闭...
ping -n 2 ::1 >nul 2>&1
rem 跳回来看看我们现在是否可以生成一个新进程
转到循环
转到:eof

:检查实例
rem 这可能可以做得更好。但 INSTANCES 应该包含之后正在运行的实例的数量。
for /f "usebackq" %%t in (`tasklist /fo csv /fi "imagename eq wait.exe"^|wc -l`) 执行设置 INSTANCES=%%t
转到:eof

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

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

@echo 关闭
标题%1
“%2”“%3”

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

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

您还可以轻松地调整它以使用 psexec 远程生成进程(但不太可行,因为您必须在另一台计算机上拥有管理员权限并在批次)。不过,您必须使用进程名称进行过滤。

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

@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.exe would 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 psexec to 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.

注定孤独终老 2024-10-21 18:56:16

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

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

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

Running Windows batch file commands asynchronously

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文