0

I have a script that does a bunch of image processing. So far it runs sequentially, uses only one core and takes forever. I have four cores at hand. How can I run four of these commands at the same time?

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

Edit: I've made a new question related to this one How to put powershell commands in an array?

Community
  • 1
  • 1
mles
  • 4,534
  • 10
  • 54
  • 94
  • take a look at http://stackoverflow.com/questions/12766174/how-to-execute-a-powershell-function-several-times-in-parallel – Robert Levy Nov 02 '14 at 02:41
  • 1
    Also take a look at RunspacePools http://thesurlyadmin.com/2013/02/11/multithreading-powershell-scripts/ – Matt Nov 02 '14 at 13:27
  • Thanks, I found the thesurlyadmin site before, but didn't know where to start. – mles Nov 02 '14 at 23:53

1 Answers1

1

This should be pretty close to what you want to do. However I cannot test it.

EDIT From Comment: I've added some logic so that only 4 jobs will run at any time.

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
Micky Balladelli
  • 9,781
  • 2
  • 33
  • 31
  • Close yeah. That starts all the jobs at once, but not 4 at a time. – mles Nov 02 '14 at 23:56
  • For some reason your modified answer doesn't start the `Convert 76 ...` and `Convert 118 ...` jobs? Tried it 2 times, but only 7 out of 9 jobs get started. No error on the console. – mles Nov 06 '14 at 23:09
  • Almost. It starts with 5 threads. If I modify the while block to `$running.count -gt 3` it runs with no more than 4 threads. A little late but thank you for your effort! – mles Nov 17 '14 at 00:07