Last active
April 4, 2017 14:53
-
-
Save MarkTiedemann/6ecfa5ce96476fd44b2b92b99c915c98 to your computer and use it in GitHub Desktop.
Run jobs in parallel using Powershell
This file contains 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
$code = { | |
param ($id) | |
$start = Get-Date | |
# your code here | |
Start-Sleep 1 | |
$stop = Get-Date | |
$total = ($stop - $start).TotalMilliSeconds | |
"[$id] $total ms" | |
} | |
$jobs = @() | |
1..4 | % { | |
$jobs += Start-Job $code -ArgumentList $_ | |
} | |
$jobs | Wait-Job | Out-Null | |
$jobs | Receive-Job | |
$jobs | Remove-Job | |
# Output: | |
# [1] 1004.0012 ms | |
# [2] 1004.0012 ms | |
# [3] 1004.0012 ms | |
# [4] 1003.9979 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment