在四个并行的核心上运行不同的Powershell命令

时间:2014-11-02 02:32:27

标签: multithreading powershell imagemagick-convert

我有一个执行大量图像处理的脚本。到目前为止它按顺序运行,只使用一个核心并且需要永久。我手头有四个核心。如何同时运行其中四个命令?

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

编辑:我已经提出了一个与此问题相关的新问题How to put powershell commands in an array?

1 个答案:

答案 0 :(得分:1)

这应该与您想要做的非常接近。但是我无法测试它。

编辑来自评论: 我添加了一些逻辑,因此任何时候都只能运行4个作业。

function Convert
{
    param ($scene, $source, $target, $density = "200", $quality = "80", $delete = "0")

    $running = @(Get-Job | ? {$_.State -eq 'Running'})
    while ($running.Count -gt 4) {
        $done = Get-Job | Wait-Job -Any
        $running = @(Get-Job | ? {$_.State -eq 'Running'})
    }

    $arguments = @($density, $quality, $delete, $scene, $source, $target)
    start-job -scriptblock { convert.exe -density $args[0] -quality $args[1] -delete $args[2] -scene $args[3] $args[4] $args[5]} -ArgumentList $arguments   

}

Convert 1  C:\Users\mles\Desktop\ta2014\v33_1_21_Northland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 22 C:\Users\mles\Desktop\ta2014\v33_22_31_Auckland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 32 C:\Users\mles\Desktop\ta2014\v33_32_49_Waikato.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 50 C:\Users\mles\Desktop\ta2014\v33_50_62_Whanganui.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 63 C:\Users\mles\Desktop\ta2014\v33_63_69_Manawatu.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 70 C:\Users\mles\Desktop\ta2014\v33_70_75_Wellington.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 76 C:\Users\mles\Desktop\ta2014\v33_76_92_Marlborough.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 93 C:\Users\mles\Desktop\ta2014\v33_93_117_Canterbury.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 118 C:\Users\mles\Desktop\ta2014\v33_118_127_Otago.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg
Convert 128 C:\Users\mles\Desktop\ta2014\v33_128_141_Southland.pdf C:\Users\mles\Desktop\ta2014\%03d.jpg

Get-Job | Wait-Job
Get-Job | Receive-Job