RamblingCookieMonster / Invoke-Parallel

Speed up PowerShell with simplified multithreading
MIT License
384 stars 88 forks source link

How does throttle work #31

Closed sameercaresu closed 8 years ago

sameercaresu commented 8 years ago

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?

artisticcheese commented 8 years ago

+1 Also confused how throttle works. All objects seems to be created at runtime and then fed to script

RamblingCookieMonster commented 8 years ago

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:

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!