问题
I have a PowerShell workflow that looks something like this, except what each Foo function does is normally very different:
function Foo1
{
"Foo1 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo2
{
"Foo2 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo3
{
"Foo3 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo4
{
"Foo4 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo5
{
"Foo5 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
function Foo6
{
"Foo6 : {0:hh}:{0:mm}:{0:ss}" -f (Get-Date)
Start-Sleep 2
}
workflow Invoke-Workflow
{
parallel
{
Foo1
Foo2
Foo3
Foo4
Foo5
Foo6
}
}
Invoke-Workflow
The output of this is:
Foo1 : 10:28:43
Foo2 : 10:28:43
Foo3 : 10:28:44
Foo5 : 10:28:44
Foo4 : 10:28:44
Foo6 : 10:28:46
Showing that the first 5 run immediately, and then the 6th item has to wait for one of the previous items to finish.
I see a lot of documentation that shows how to increase the number of parallel executions in a foreach loop. However how do I increase the number of items that will run in a parallel block?
回答1:
There doesn't appear to be a way to increase the number of threads that will be used in a parallel block, or if there is, it's difficult to find. A workaround would be to use the Start-Job command. You can use
Start-Job -Scriptblock {#do stuff here}
Or, if it is more code than you want to have inline, you can have it run a script:
Start-Job -Filepath "c:\temp\job.ps1" -ArgumentList 1
It takes some time for each job to spin up, so they don't all start simultaneously. I ran a loop to run Start-Job commands and log the time they started. Each job had a 30 second sleep so that it wouldn't just die immediately (and try to force the number of jobs running to hit the limit of threads). Results varied somewhat, but testing up to 20 showed that it is able to run at least that many simultaneously. In the results below, some were started 28 seconds after the first ones, but remember that the first ones were still running due to the 30 second sleep. So they didn't all start rapid fire, but there were 20 running at once.
#1 started at 5/29/2015 2:17:25 PM
#2 started at 5/29/2015 2:17:25 PM
#3 started at 5/29/2015 2:17:26 PM
#4 started at 5/29/2015 2:17:26 PM
#5 started at 5/29/2015 2:17:27 PM
#7 started at 5/29/2015 2:17:28 PM
#6 started at 5/29/2015 2:17:28 PM
#8 started at 5/29/2015 2:17:31 PM
#9 started at 5/29/2015 2:17:37 PM
#10 started at 5/29/2015 2:17:47 PM
#12 started at 5/29/2015 2:17:53 PM
#13 started at 5/29/2015 2:17:53 PM
#15 started at 5/29/2015 2:17:53 PM
#18 started at 5/29/2015 2:17:53 PM
#11 started at 5/29/2015 2:17:53 PM
#20 started at 5/29/2015 2:17:53 PM
#14 started at 5/29/2015 2:17:53 PM
#16 started at 5/29/2015 2:17:53 PM
#17 started at 5/29/2015 2:17:53 PM
#19 started at 5/29/2015 2:17:53 PM
I did run across a couple of other methods for multi-threading with PowerShell, but this was the easiest to understand and demonstrate. It is probably not the best.
来源:https://stackoverflow.com/questions/30533050/powershell-workflow-thread-limit