Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save countzero/87ef1a5e187c7479972576f6a282e8d3 to your computer and use it in GitHub Desktop.
Save countzero/87ef1a5e187c7479972576f6a282e8d3 to your computer and use it in GitHub Desktop.
Execute PowerShell jobs in parallel with a max concurrency using the Wait-Job strategy.
$maxConcurrentJobs = 5
for ($index=1; $index -le 23; $index++) {
$runningJobs = $(Get-Job -State Running)
$concurrencyLimitIsReached = $($runningJobs.Count -ge $maxConcurrentJobs)
if ($concurrencyLimitIsReached) {
Write-Host "Reached concurrency limit of ${maxConcurrentJobs}, waiting for a job to complete..."
$runningJobs | Wait-Job -Any | Out-Null
}
$scriptBlock = {
Param (
[string] [Parameter(Mandatory=$true)] $id
)
$durationInMilliseconds = $(Get-Random -Minimum 500 -Maximum 1000)
Start-Sleep -Milliseconds $durationInMilliseconds
Write-Host "Job #${id} completed in ${durationInMilliseconds} ms."
}
Write-Host "Starting Job #${index}..."
Start-Job $scriptBlock -Name "Job #${index}" -ArgumentList $index | Out-Null
}
Get-Job | Receive-job -AutoRemoveJob -Wait
@countzero
Copy link
Author

This will output the job results after all jobs have been completed in the order they were started:

Starting Job #1...
Starting Job #2...
Starting Job #3...
Starting Job #4...
Starting Job #5...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #6...
Starting Job #7...
Starting Job #8...
Starting Job #9...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #10...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #11...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #12...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #13...
Starting Job #14...
Starting Job #15...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #16...
Starting Job #17...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #18...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #19...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #20...
Starting Job #21...
Starting Job #22...
Reached concurrency limit of 5, waiting for a job to complete...
Starting Job #23...
Job #1 completed in 698 ms.
Job #2 completed in 866 ms.
Job #3 completed in 510 ms.
Job #4 completed in 866 ms.
Job #5 completed in 507 ms.
Job #6 completed in 963 ms.
Job #7 completed in 579 ms.
Job #8 completed in 996 ms.
Job #9 completed in 798 ms.
Job #10 completed in 820 ms.
Job #11 completed in 872 ms.
Job #12 completed in 580 ms.
Job #13 completed in 833 ms.
Job #14 completed in 865 ms.
Job #15 completed in 903 ms.
Job #16 completed in 658 ms.
Job #17 completed in 743 ms.
Job #18 completed in 630 ms.
Job #20 completed in 737 ms.
Job #19 completed in 960 ms.
Job #21 completed in 846 ms.
Job #22 completed in 738 ms.
Job #23 completed in 814 ms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment