Created
April 17, 2020 19:42
-
-
Save jahands/be721ac325b6ac410a34ddc8a932f06b to your computer and use it in GitHub Desktop.
Run things in parallel in PowerShell 7
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
Function Foo { | |
@(1..2) | ForEach-Object -Parallel { | |
switch ($_) { | |
1 { | |
Return 1 | |
} | |
2 { | |
# This could be something that takes a while | |
# like a network call, etc | |
Start-Sleep 3 | |
Write-Warning 'Finished Foo' | |
} | |
} | |
} | |
} | |
Function Bar { | |
Foo | ForEach-Object { | |
$a = $_ | |
@(1..2) | ForEach-Object -Parallel { | |
switch ($_) { | |
1 { | |
$r = "$($_), 2" | |
Return $r | |
} | |
2 { | |
Start-Sleep 3 | |
Write-Warning 'Finished Bar' | |
} | |
} | |
} | |
} | |
} | |
Bar | ForEach-Object { | |
# This way we get to use the value | |
# asap while all the defered methods run | |
$x = $_ | |
# Use $x | |
Write-Host $x | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment