问题
I got a scirpt, that is working fine, but I want to speed up it a little, by using multithreading. The problem is, that it's kinda difficult to do it, without changing a lot of code. I would love to avoid jobs with new separated scopes, but it looks like it's impossible. Also starting-job is really slow, it needs ~~150miliseconds to start execution. Is there any way to avoid jobs, or atleast make this script block work, as expected? (no doubled functions, passing arguments correctly)
Param(
[int]$arg1 = 2, [int]$arg2 = 3
)
$functionDoSomething = {
function doSomething($inp) {
return $inp + 10
}
}
function doSomething($inp) {
return $inp + 10
}
# time: 1523337 ticks
Measure-Command {
Start-Job -Name Job1 -InitializationScript $functionDoSomething -ScriptBlock {
return doSomething ($arg1 + $arg2)
} -ArgumentList $arg1, $arg2
Wait-Job Job1
$var2 = Receive-Job Job1 # result is 10, so arguements aren't passed correctly, trick with @($arg1, arg2) = the same result
}
# time: 6867 ticks
Measure-Command {
$var3 = doSomething ($arg1 + $arg2) # result is 15
}
回答1:
# Have no fear - Invoke-Parallel is here. Hoping that I can help steer you in the right direction. Let me show you some pseudo code that can point you in the right direction.
# You need a list of objects in which you want to perform an operation for
$listOfComputers = @(
'1'
'2'
'3'
)
# Pipe your array to Invoke-Parallel which takes care of opening threads depending on the amount of objects piped in.
# If you want to control the amount of simultaneous threads simply help Invoke-Parallel -ShowWindow to see how
$results = $listOfComputers | Invoke-Parallel -ScriptBlock {
function doSomething($inp)
{
return [int]$inp + 10
}
if ($_ -eq '1') { Start-Sleep -Seconds 15; Write-Verbose 'Waited 15s' } # Adding this so you can see that this script block is running in a multi-threaded fashion
# $_ refers to the values inside of $listOfComputers
return doSomething ($_)
} -Verbose
$results
<# Notice how 11 comes up last
12
13
11
#>
回答2:
First, look into Runspaces and a RunSpacePool. When you create a runspace you can specify which modules get loaded other than the default. See here http://www.get-blog.com/?p=189 for more details.
$ScriptBlock = {
param([string]$forinput)
"Do something with $forinput"
[PSCusomObject]@{
Line1="Something"
Line2="Something done to $forinput"
}#end of custom object output
}#end script block
#Create Runspace pool, 100 min and max.
#Example data contained within $item.property
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(100,100)
$RunspacePool.Open()
$Jobs =
foreach ( $item in $VariableContainsListOfInput)
{
$Job = [powershell]::Create().
AddScript($ScriptBlock).
AddArgument($item.property)
$Job.RunspacePool = $RunspacePool
[PSCustomObject]@{
Pipe = $Job
Result = $Job.BeginInvoke()
}
}
DO{Start-sleep -seconds 1
}While($Jobs.Result.IsCompleted -contains $false)
#Loop through jobs, pipe EndInvoke data from runspace into Export-CSV
$(ForEach ($Job in $Jobs)
{ $Job.Pipe.EndInvoke($Job.Result) }) |
Export-Csv -Path "$FullPath\$file" -NoTypeInformation
#clean up runspace
$RunspacePool.Close()
$RunspacePool.Dispose()
来源:https://stackoverflow.com/questions/39180266/powershell-adding-multithreading-to-finished-script