Doing Things in Parallel

by Jul 8, 2016

Any version

By processing things in parallel rather than sequential, a script can complete much faster. Here is an example that uses background jobs to execute three tasks in parallel:

# three things to do...
$task1 = { Start-Sleep -Seconds 5 1 }
$task2 = { Start-Sleep -Seconds 7 2 }
$task3 = { Start-Sleep -Seconds 3 3 }

# do two things in the background...
$job1 = Start-Job -ScriptBlock $task1
$job3 = Start-Job -ScriptBlock $task3

# do one thing in our process...
$resultTask2 = & $task2

# wait for the other two background tasks to complete...
$null = Wait-Job -Job $job1, $job3

# get the results from these background tasks...
$resultTask1 = Receive-Job -Job $job1
$resultTask3 = Receive-Job -Job $job3

# clean up...
Remove-Job -Job $job1, $job3

# here we go with all three task results
$resultTask1
$resultTask2
$resultTask3

Just be aware that background jobs need to serialize their results to transfer them back to you. They are most efficient if the background task can complete the job without the need to transfer lots of data back to the foreground process.

Twitter This Tip! ReTweet this Tip!