Welcome › Forums › General PowerShell Q&A › Multithreading Script
- This topic has 1 reply, 1 voice, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
December 4, 2020 at 12:02 pm #276594
I am attempting to multithread a script and any time I increase the amount of threads, the performance of the script greatly decreases. For example, if I set a max of 5 threads I can write 635 objects/min. When I set to 10 threads, I can write only 361 objects/min. I have beefed up resources on the server I am running this from and never get close to maxing out the hardware, so I know it’s not a hardware limitation.
Here is the script:
PowerShell12345678910111213141516171819202122232425262728293031323334353637$runspacecollection = @()$runspacepool = [runspacefactory]::CreateRunspacePool(1, 5)$runspacePool.Open()$scriptblock = {param($folder)Script goes here}Write-Host ‘Parsing Folder Structure’$folders = Get-ChildItem \\server\DatastoreWrite-Host ‘Processing Files’foreach ($folder in $folders) {$powershell = [powershell]::Create().AddScript($scriptblock).AddArgument($folder)$powershell.RunspacePool = $runspacepool[Collections.ArrayList]$runspacecollection += New-Object -TypeName System.Management.Automation.PSObject -Property @{runspace = $powershell.BeginInvoke()powershell = $powershell}}while ($runspacecollection) {foreach ($runspace in $runspacecollection.ToArray()) {if ($runspace.Runspace.IsCompleted) {$runspace.Powershell.EndInvoke($runspace.Runspace)$runspace.Powershell.Dispose()$runspacecollection.Remove($runspace)}}}-
This topic was modified 1 month, 2 weeks ago by
kvprasoon. Reason: Code formatting https://powershell.org/forums/topic/guide-to-posting-code-2/
-
This topic was modified 1 month, 2 weeks ago by
-
December 4, 2020 at 4:19 pm #276684
After checking various settings and syntax, my issue is with the server that I am reading files from. I can see that I am getting all the threads, however, I am being bottlenecked by the read speed on the server I am reading from.
The more threads the bigger the read speed bottleneck. Explains a lot.
-
-
AuthorPosts
- You must be logged in to reply to this topic.