我有一个执行大量图像处理的脚本。到目前为止,它是按顺序运行的,只使用一个核心并且需要很长时间。我手头有四个核心。我想同时运行其中四个命令。为了实现这一点,我需要将命令放在一个数组中。这些是原始命令:
convert.exe -density 200 -quality 80 -delete 0 -scene 1 C:\Users\mles\Desktop\ta2014\v33_1_21_Northland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 22 C:\Users\mles\Desktop\ta2014\v33_22_31_Auckland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 32 C:\Users\mles\Desktop\ta2014\v33_32_49_Waikato.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 50 C:\Users\mles\Desktop\ta2014\v33_50_62_Whanganui.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 63 C:\Users\mles\Desktop\ta2014\v33_63_69_Manawatu.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 70 C:\Users\mles\Desktop\ta2014\v33_70_75_Wellington.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 76 C:\Users\mles\Desktop\ta2014\v33_76_92_Marlborough.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 93 C:\Users\mles\Desktop\ta2014\v33_93_117_Canterbury.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 118 C:\Users\mles\Desktop\ta2014\v33_118_127_Otago.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
convert.exe -density 200 -quality 80 -delete 0 -scene 128 C:\Users\mles\Desktop\ta2014\v33_128_141_Southland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
到目前为止,我想出了这个:
$array = @()
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 1 C:\Users\mles\Desktop\ta2014\v33_1_21_Northland.pdf C:\Users\mles\Desktop\ta2014\%03d_test.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 22 C:\Users\mles\Desktop\ta2014\v33_22_31_Auckland.pdf C:\Users\mles\Desktop\ta2014\%03d_test.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 32 C:\Users\mles\Desktop\ta2014\v33_32_49_Waikato.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 50 C:\Users\mles\Desktop\ta2014\v33_50_62_Whanganui.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 63 C:\Users\mles\Desktop\ta2014\v33_63_69_Manawatu.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 70 C:\Users\mles\Desktop\ta2014\v33_70_75_Wellington.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 76 C:\Users\mles\Desktop\ta2014\v33_76_92_Marlborough.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 93 C:\Users\mles\Desktop\ta2014\v33_93_117_Canterbury.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 118 C:\Users\mles\Desktop\ta2014\v33_118_127_Otago.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
$array += "convert.exe -density 200 -quality 80 -delete 0 -scene 128 C:\Users\mles\Desktop\ta2014\v33_128_141_Southland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg"
foreach ($element in $array) {
$MaxThreads = 4
While (@(Get-Job | Where { $_.State -eq "Running" }).Count -ge $MaxThreads)
{ Write-Host "Waiting for open thread...($MaxThreads Maximum)"
Start-Sleep -Seconds 1
}
Start-Job -Scriptblock{ $element }
}
While (@(Get-Job | Where { $_.State -eq "Running" }).Count -ne 0)
{ Write-Host "Waiting for background jobs..."
Get-Job #Just showing all the jobs
Start-Sleep -Seconds 1
}
ForEach ($Job in (Get-Job)) {
Remove-Job $Job
}
工作开始但立即退出。我想这与我如何在数组中存储命令有关?