Last active
November 2, 2016 16:10
-
-
Save jrotello/2e42a42f5a40c446708b330c5d7f461f to your computer and use it in GitHub Desktop.
PowerShell Jobs Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
$MaxJobs = $env:NUMBER_OF_PROCESSORS | |
) | |
$elapsed = [System.Diagnostics.Stopwatch]::StartNew() | |
$jobs = @() | |
1..25 | % { | |
$running = Get-Job -State Running | |
if (($running | Measure-Object).Count -ge $MaxJobs) { | |
"Waiting..." | |
Wait-Job -Job $running -Any | Out-Null | |
} | |
$i = $_ | |
"Creating job #$($i)" | |
$jobs += Start-Job { | |
Start-Sleep -Seconds 3 | |
"Finished job #$($using:i)" | |
} | |
} | |
"Waiting for jobs to complete..." | |
Wait-Job $jobs | Out-Null | |
"Receiving jobs..." | |
$jobs | Receive-Job | |
"Removing jobs..." | |
$jobs | Remove-Job | |
"Done. $($elapsed.ElapsedMilliseconds)ms" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment