0

In node.js you can kick off several asynchronous functions at one time and then have them return the results to a function when they're all complete: understanding-node-js-async-parallel

In PowerShell I've seen a similar functionality using Start-Job but! when actually trying to run the tasks they seem to lag too much to be non-blocking and being started in parallel:

   Write-Host "Running jobs $($start) - $($end)"
   for($i = [int]$start; $i -le $end; $i++) {
     $jobResults += Start-Job -ScriptBlock $someFunc | wait-job | receive-job
   }

I believe this stems from the wait-job function which waits for the kicked off job to complete before receiving it.

Is there a way to wait for all of them and receive them into an array like node async does? Or am I looking at apples and oranges here?

Mark
  • 143,421
  • 24
  • 428
  • 436
leeand00
  • 25,510
  • 39
  • 140
  • 297
  • 1
    see https://stackoverflow.com/questions/53331116/when-using-powershell-jobs-runspaces-or-workflows-are-the-threads-being-execu/53332253#53332253 and use Get-Help for all the cmdlets used there. – Kory Gill Jan 03 '19 at 03:17
  • Maybe you are looking for a [Runspacepool](https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.runspaces.runspacepool?view=powershellsdk-1.1.0) better explained [here](https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/28/beginning-use-of-powershell-runspaces-part-3/). You create a bunch of runspaces (let's say 10) and throw a lot of work at them (100x) every runspace will do its stuff and when finished it will take the next job till everything is done... In the end one my have done 9 tasks the other 12 but everything is done... – T-Me Jan 03 '19 at 09:03

1 Answers1

1

Your snippet is actually running jobs synchronously. To run them async do something like this:

$jobs = {sleep 5; Write-Output "job1"}, {sleep 6; Write-Output "job2"}, {sleep 7; Write-Output "job3"}

$result = $jobs | foreach {  start-job $_ } | wait-job | receive-job

Although Start-Job will create a new process (probably not the best option if your jobs run just few seconds). If you need to run jobs in separate threads you'll need to use runspaces. You can code it manually, but there are some tools for that as well (like Invoke-Parallel)

Mike Twc
  • 2,230
  • 2
  • 14
  • 19