Spack
Spack

Reputation: 494

Use of RunspacePool shows sequential output

I'm discovering parallel computing in Powershell but there is something I don't get. Even if I'm using a RunspacePool, the output of my tasks is shown sequentially. Take the following code:

Param(
    [int]$MaxJobs = 3
)


Begin {
    $Jobs = @()
    $JobQueue = [System.Collections.Queue]::Synchronized(
        (New-Object System.Collections.Queue)
    )

    if ($MaxJobs -lt 0) {
        $MaxJobs = 1
    }
    if ($MaxJobs -eq 0) {
        $MaxJobs = 50
    }

    $JobPool = [RunspaceFactory]::CreateRunspacePool(1, $MaxJobs)
    $JobPool.Open()
}


Process {
    for ($i = 0; $i -lt 50; $i++)
    {
        $script = '$t = Get-Random -Minimum 1 -Maximum 10; "' + $i + ': $t" | Out-File jobs.out -Append; sleep $t;'

        $thread = [powershell]::Create()
        $thread.AddScript($script) | Out-Null
        $thread.RunspacePool = $JobPool
        $handle = $thread.BeginInvoke()

        $j = "" | Select-Object Handle, Thread
        $j.Handle = $handle
        $j.Thread = $thread
        $Jobs += $j
    }
}


End {
    while (@($Jobs | Where-Object {$_.Handle -ne $Null}).Count -gt 0) {
        foreach ($j in $($Jobs | Where-Object {$_.Handle.IsCompleted -eq $True})) {
            $j.Thread.EndInvoke($j.Handle)
            $j.Thread.Dispose()
            $j.Thread = $Null
            $j.Handle = $Null
        }
    }
    $JobPool.Close() | Out-Null
    $JobPool.Dispose() | Out-Null
}

In the Process block, given the random sleeping value, I would expect the file to be written quite randomly also but this is not the case.

 0 :   7 
 1 :   1  
 2 :   1  
 3 :   8  
 4 :   8  
 5 :   2  
 6 :   7  
 7 :   9  
 8 :   9  
 9 :   9  
 1 0 :   3
 1 1 :   3
 1 2 :   5
 1 3 :   5
 1 4 :   3
 1 5 :   1
 1 6 :   1
 1 7 :   9
 1 8 :   9
 1 9 :   1
 2 0 :   8
 2 1 :   1
 2 2 :   1
 2 3 :   1
 2 4 :   1
 2 5 :   1
 2 6 :   1
 2 7 :   8
 2 8 :   8
 2 9 :   9
 3 0 :   8
 3 1 :   8
 3 2 :   1
 3 3 :   6
 3 4 :   1
 3 5 :   1
 3 6 :   5
 3 7 :   5
 3 8 :   5
 3 9 :   9
 4 0 :   3
 4 1 :   3
 4 2 :   5
 4 3 :   5
 4 4 :   4
 4 5 :   4
 4 6 :   4
 4 7 :   1
 4 8 :   1
 4 9 :   4

Is this a normal behavior?

Upvotes: 1

Views: 128

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Take a good look at the statements you're running in the runspace pool:

$script = '$t = Get-Random -Minimum 1 -Maximum 10; "' + $i + ': $t" | Out-File jobs.out -Append; sleep $t;'

or, nicely formatted:

$t = Get-Random -Minimum 1 -Maximum 10
"$i: $t" | Out-File jobs.out -Append
Sleep $t

You write to the file before sleeping, so of course the duration of the Start-Sleep command is not going to affect the order of writing to the file.

If you want to see the effects of the sleep, put the Out-File statement last:

$script = '$t = Get-Random -Minimum 1 -Maximum 10; sleep $t;"' + $i + ': $t" | Out-File jobs.out -Append;'

Upvotes: 2

Related Questions