Limiting PowerShell Workflow Flow

I have a PowerShell workflow that looks something like this, except that each Foo function is usually 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 result of this:

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 are fired immediately and then the 6th item should wait for one of the previous items to finish.

I see a lot of documentation that shows how to increase the number of concurrent executions in a foreach loop. However, how to increase the number of elements that will be executed in a parallel block?

+3


source to share


1 answer


There seems to be no way to increase the number of threads to be used in a parallel block, or if there is, it's hard to find. A workaround would be to use the Start-Run command. you can use

Start-Job -Scriptblock {#do stuff here}

Or, if this is more code than you want inline, you can run a script of it:

Start-Job -Filepath "c:\temp\job.ps1" -ArgumentList 1



Each task takes some time, so they don't all start at the same time. I ran a loop to run Start-Job commands and register when they were run. Each job had a 30 second sleep so it doesn't die right away (and try to get the number of jobs to work to reach the thread limit). The results varied slightly, but testing up to 20 showed that it can run at least many at the same time. In the results below, some of them were started 28 seconds after the former, but remember that the former were still started due to 30 seconds of sleep. Thus, they did not all start rapid fire, but there were two moves 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 have come across several other multithreading techniques with PowerShell, but this was the easiest to understand and demonstrate. This is probably not the best.

+1


source







All Articles