0

我有一个脚本,它工作得很好,但我想通过使用多线程来加快它的速度。问题是,在不更改大量代码的情况下很难做到这一点。我很想避免使用新的分离范围的工作,但看起来这是不可能的。启动作业也很慢,开始执行需要〜150毫秒。有什么办法可以避免工作,或者至少使这个脚本块按预期工作?(没有双重功能,正确传递参数)

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
}
4

2 回答 2

0
# 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
#>
于 2016-08-27T11:07:01.560 回答
0

首先,查看 Runspaces 和 RunSpacePool。创建运行空间时,您可以指定加载哪些模块而不是默认模块。有关详细信息,请参见此处http://www.get-blog.com/?p=189 。

$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()
于 2016-08-27T15:53:40.120 回答