Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active March 27, 2025 10:06
Show Gist options
  • Save gitfvb/50ec5833d552daa69e10f95003eb1e9a to your computer and use it in GitHub Desktop.
Save gitfvb/50ec5833d552daa69e10f95003eb1e9a to your computer and use it in GitHub Desktop.
Parallelisation with PowerShell and Jobs
# Remove previous jobs first
Get-Job | Remove-Job
$runs = 100
$maxThreads = 10
$start = [datetime]::Now
$c = 0 # completed jobs
$sum = 0 # sum of results
for ( $i = 0; $i -lt $runs; $i++ ) {
# Automatic throttling
while ( @( Get-Job -State Running ).Count -ge $maxThreads ) {
# Check current job status while waiting
Get-Job -State Completed | Receive-Job -AutoRemoveJob -Wait | ForEach-Object {
$c += 1
$sum += $_
"Returned $( $c ) jobs"
}
Start-Sleep -Milliseconds 200
}
# put a variable like `$j =` in front or `[void]()` around to suppress output
#[void](
Start-Job {
Start-Sleep -Seconds 5
If ( $using:i -eq 3 ) {
throw "Oh no!"
}
$using:i # using a variable from "outside"
}
#)
}
# Wait and get the last results
Get-Job | Wait-Job | Where-Object { $_.State -eq "Completed" } | Receive-Job -AutoRemoveJob -Wait | ForEach-Object {
$c += 1
$sum += $_
}
# Check if there are more jobs left to check
Get-Job | ForEach-Object {
$_
}
# Check the result with
$sumCheck = 0; 0..($runs - 1) | % { $sumCheck += $_ }; $sumCheck -= 3 # because the 3rd job throws an exception
"Total sum of $( $sum )"
If ( $sum -eq $sumCheck ) {
"The result is correct"
} else {
"The result is not correct"
}
$ts = New-TimeSpan -Start $start -end ( [datetime]::Now )
"Total: $( $ts.TotalSeconds ) seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment