When using Powershell Jobs, Runspaces, or Workflows, are the threads being executed on separate cores? When using Powershell Jobs, Runspaces, or Workflows, are the threads being executed on separate cores? powershell powershell

When using Powershell Jobs, Runspaces, or Workflows, are the threads being executed on separate cores?


I would say yes if by separate cores, you mean logical cores (not physical cores). Run something like this and notice the output from “$num”. $num represents the current logical (not physical) core on which a thread is running. If this script runs on a dual core machine (with 4 logical cores), the output of $num is a 0, 1, 2, or 3. See here for a better explanation on Logical vs Physical CPU.

$sb = {    param($sbarg)$MethodDefinition = @'[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]public static extern int GetCurrentProcessorNumber();'@    $Kernel32 = Add-Type -MemberDefinition $MethodDefinition -Name 'Kernel32' -Namespace 'Win32' -PassThru    0..10 | % {    $num = $Kernel32::GetCurrentProcessorNumber()     Write-Output "[$sbarg]:[$_] on '$num'"    # simulate some work that may make the cpu busy    # perhaps watch in task manager as well to see load    $result = 1; foreach ($number in 1..1000000) {$result = $result * $number};    Start-Sleep -Milliseconds 500    }}0..10 | % {    Start-Job -ScriptBlock $sb -ArgumentList $_}Get-Job# Wait for it all to completeWhile (Get-Job -State "Running"){    Write-Output "waiting..."    Start-Sleep 2}# Getting the information back from the jobsGet-Job | Receive-Job# Clean UpGet-Job | Remove-Job