How does throttle work
sameercaresu opened this issue · 2 comments
I am a bit confused how "throttle" is supposed to work.
I have a code where I have to do web requests to various endpoints. I was hoping that using Invoke-Parallel would make it faster but no luck
Invoke-Parallel -InputObject $inputObject -ImportVariables -ImportModules -Throttle 10 { // do some web requests to endpoints contained in inputObject }
Where $inputObject is object array size >= 1000
But when I run in ISE I can see "Starting threads" going > 10. At some point ISE just freezes. I understood "Throttle" is the number of threads running parallel at one time and it will never exceed the given limit. Probably I am doing something wrong or not using some other parameters?
+1
Also confused how throttle works. All objects seems to be created at runtime and then fed to script
Hi!
Apologies for the delay, thanks for pinging me on this @artisticcheese.
TLDR analogy: $Throttle is how many cashiers we have working
, and we have some enforcer preventing people from even queuing up if $MaxQueue
folks are already in line.
Longer story:
So! There's two pieces. There's the Throttle, or, how many things can execute at once, and MaxQueue, or how many PowerShell instances to add to the runspace pool at once.
The MaxQueue default varies:
- If
$RunspaceTimeout
is specified, it is set to the value of$Throttle
, to ensure we only time out items that have been running longer than the timeout - If
$RunspaceTimeout
is not specified, we are a little less concerned about the accuracy of the time keeping, so we set the default to$Throttle * 3
The code that implements starts here. Finally, in the end
block, we loop through all the $InputObject
's from the user, and in that loop, we check to see if we've exceeded $MaxQueue
- if so, we sleep a bit, check again until we have room for more.
Feel free to flip through the code for more detail. I suspect if your ISE is freezing, you may be running into a bug of some sort - perhaps in the systems you're requesting from, perhaps in the logic behind the calls, etc. Or, it could be bad logic on this side - if you find any bugs, please let me know!
Cheers!