multithreading - How to put powershell commands in an array? -
i have script bunch of image processing. far runs sequentially, uses 1 core , takes forever. have 4 cores @ hand. want run 4 of these commands @ same time. create happen, need set commands in array. these original commands:
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
so far came this:
$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 | { $_.state -eq "running" }).count -ge $maxthreads) { write-host "waiting open thread...($maxthreads maximum)" start-sleep -seconds 1 } start-job -scriptblock{ $element } } while (@(get-job | { $_.state -eq "running" }).count -ne 0) { write-host "waiting background jobs..." get-job #just showing jobs start-sleep -seconds 1 } foreach ($job in (get-job)) { remove-job $job }
the jobs start quit immediately. guess has how i'm storing commands in array?
you may want take @ workflows bump against max of 5 concurrent processes. there several examples of using runspace pools , powershell implement multi threading in manner bypasses limit. gave working illustration here.
alternatively store scriptblocks scriptblocks in array instead of strings.
$commands = @() $commands += {echo "test1"} $commands += {echo "test2"} $commands += {echo "test3"} foreach($command in $commands){ start-job $command }
arrays multithreading powershell imagemagick-convert start-job
No comments:
Post a Comment