本文介绍了可以在cmd中启动多个线程的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

但是,当我做 optipng a * .png

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

推荐答案

我写了一个批处理文件,在一段时间之前只能执行最大数量的命令::

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.

没有办法正确计算由此产生的进程这个批次,虽然。一种方法是在批处理开始时创建一个随机数(%RANDOM%),创建一个帮助批处理,执行处理(或产生处理程序),但可以将其窗口标题设置为参数:

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"

这是一个简单的批处理,然后运行第二个参数,第三个作为参数。然后,您可以通过仅选择具有指定窗口标题( tasklist / fiwindowtitle eq ...)的进程来过滤任务列表。这应该工作相当可靠,并防止太多的假阳性。搜索 cmd.exe 将是一个坏主意,如果你还有一些实例正在运行,因为这限制了你的工作进程池。

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.

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

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

你也可以很容易地适应这使用 psexec 来远程产生进程(但不会是非常可行,因为你必须有在其他机器上的管理员权限,以及在批处理中提供密码)。然而,你必须使用进程名称进行过滤。

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.

这篇关于可以在cmd中启动多个线程的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:31