在Powershell中运行的启动进程的限制数量 [英] Limit number of Start-Process running in powershell
问题描述
我试图限制通过Powershell运行的 Start-Process
的数量,但是我似乎无法使其正常工作.
I have tried to limit the number of Start-Process
running from a Powershell, but I can't seem to get it to work.
我尝试遵循此过程: https://exchange12rocks.org/2015/05/24/how-to-limit-a-number-of-powershell-jobs-running-同时运行/和在powershell中运行N个并行作业
I tried to follow this process: https://exchange12rocks.org/2015/05/24/how-to-limit-a-number-of-powershell-jobs-running-simultaneously/ and Run N parallel jobs in powershell
但是这些是针对Jobs而不是Processes的,我想从 Start-Process
But these are for Jobs not Processes, and I would like to remove the -Wait
from the Start-Process
我对脚本的担心是,如果文件夹中有1000个音频文件,则FFMpeg会使系统崩溃.
My concern with the script is that if there are 1000 audio files in the folder, then FFMpeg would crash the system.
# get the folder for conversion
function mbAudioConvert {
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.Application]::EnableVisualStyles()
$fileBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$fileBrowser.SelectedPath = "B:\"
$fileBrowser.ShowNewFolderButton = $false
$fileBrowser.Description = "Select the folder with the audio which you wish to convert to Avid DNxHD 120 25P 48kHz"
$mbLoop = $true
$mbCount = 0001
$mbMaxJob = 4
while( $mbLoop ) {
if( $fileBrowser.ShowDialog() -eq "OK" ) {
$mbLoop = $false
$mbImage = ( Get-Item -Path "C:\Users\user\Desktop\lib\AudioOnly.jpg" )
$mbff32 = ( Get-Item -Path "C:\Users\user\Desktop\lib\ffmpeg32.exe" )
$mbff64 = ( Get-Item -Path "C:\Users\user\Desktop\lib\ffmpeg64.exe" )
$mbFolder = $fileBrowser.SelectedPath
$mbItemInc = ( ls $mbFolder\* -Include *.mp3, *.MP3, *.wav*, *.WAV*, *.ogg, *.OGG, *.wma, *.WMA, *.flac, *.FLAC, *.m4a, *.M4a )
$mbProgress = ( Get-ChildItem -Path $mbItemInc )
$mbHasRaw = ( $mbFolder + "\RAW" )
if( !( Test-Path -Path $mbHasRaw ) ) {
# force create a RAW folder if it does not exist
New-Item -ItemType Directory -Force -Path "$mbHasRaw"
}
foreach( $mbItem in $mbItemInc ) {
$mbCheck = $false
# output the progress
# Suggestion: You might want to consider updating this after starting the job and do the final update after running ex. Get-Job | Wait-Job to make the progress-bar stay until all processes are finished
#Write-Progress -Activity "Counting files for conversion" -status "Currently processing: $mbCount" -percentComplete ($mbCount / $mbItemInc.count*100)
# limit the run number
while ($mbCheck -eq $false) {
if( (Get-Job -State 'Running').count -lt $mbMaxJob) {
$mbScriptBlock = {
$mbItemName = $using:mbItem.BaseName
$mbNewItem = ( $using:mbFolder + "\RAW\" + $mbItemName + ".mov" )
$mbArgs = " -loop 1 -i $using:mbImage -i $using:mbItem -shortest -c:v dnxhd -b:v 120M -s 1920x1080 -pix_fmt yuv422p -r 25 -c:a pcm_s16le -ar 48k -af loudnorm=I=-12 $mbNewItem"
Start-Process -FilePath $using:mbff32 -ArgumentList $mbArgs -NoNewWindow -Wait
}
Start-Job -ScriptBlock $mbScriptBlock
#The job-thread doesn't know about $mbCount, better to increment it after starting the job
$mbCount++
$mbCheck = $true
}
}
}
} else {
$mbResponse = [System.Windows.Forms.MessageBox]::Show("You have exited out of the automation process!", "User has cancelled")
if( $mbResponse -eq "OK" ) {
return
}
}
}
$fileBrowser.SelectedPath
$fileBrowser.Dispose()
}
# call to function
mbAudioConvert
推荐答案
- 您编辑了
$ mbCheck
,但是while循环正在测试$ Check
,这意味着while循环将永远不会执行为$ Check -eq $ false
在未定义$ Check
的情况下为$ false
- 在作业脚本块之外创建的变量需要作为参数传递,或者您需要使用
using:
变量范围将其传递(PowerShell 3.0或更高版本).在示例中,将其添加到$ mbItem
,$ mbff32
,$ mbImage
和$ mbFolder
中. 未定义 -
$ mbMaxJob
.开始运行作业检查永远不会为真,也不会启动任何进程 -
$ mbCount
未定义.进度栏不起作用 -
echo"$ mbCount.$ mbNewItem"
不会返回任何内容,除非您在某个时候使用Receive-Job
从作业中获取输出
- You edit
$mbCheck
, but the while loop is testing$Check
which means the while-loop will never execute as$Check -eq $false
is$false
when$Check
is not defined - Variables created outside the job script-block needs to be passed as an argument or you need to use the
using:
variable-scope to pass them in (PowerShell 3.0 or later). Added it to$mbItem
,$mbff32
,$mbImage
and$mbFolder
in the example which were not defined. $mbMaxJob
is not defined. The get running jobs-check will never be true and no processes will start$mbCount
not defined. Progress bar won't workecho "$mbCount. $mbNewItem"
won't return anything unless you useReceive-Job
at some point to get the output from a job
尝试:
#DemoValues
$mbItemInc = 1..10 | % { New-Item -ItemType File -Name "File$_.txt" }
$mbff32 = "something32"
$mbFolder = "c:\FooFolder"
$mbImage = "BarImage"
$mbMaxJob = 2
$mbCount = 0
foreach( $mbItem in $mbItemInc ) {
$mbCheck = $false
# output the progress
# Suggestion: You might want to consider updating this after starting the job and do the final update after running ex. Get-Job | Wait-Job to make the progress-bar stay until all processes are finished
Write-Progress -Activity "Counting files for conversion" -status "Currently processing: $mbCount" -percentComplete ($mbCount / $mbItemInc.count*100)
# limit the run number
while ($mbCheck -eq $false) {
if ((Get-Job -State 'Running').count -lt $mbMaxJob) {
$mbScriptBlock = {
Param($mbItem, $mbFolder, $mbImage, $mbff32)
#Filename without extension is already available in a FileInfo-object using the BaseName-property
$mbItemName = $mbItem.BaseName
$mbNewItem = ( $mbFolder + "\RAW\" + $mbItemName + ".mov" )
$mbArgs = "-loop 1 -i $mbImage -i $mbItem -shortest -c:v dnxhd -b:v 120M -s 1920x1080 -pix_fmt yuv422p -r 25 -c:a pcm_s16le -ar 48k -af loudnorm=I=-12 $mbNewItem"
Start-Process -FilePath $mbff32 -ArgumentList $mbArgs -NoNewWindow -Wait
}
Start-Job -ScriptBlock $mbScriptBlock -ArgumentList $mbItem, $mbFolder, $mbImage, $mbff32
#The job-thread doesn't know about $mbCount, better to increment it after starting the job
$mbCount++
$mbCheck = $true
}
}
}
这篇关于在Powershell中运行的启动进程的限制数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!