繁体   English   中英

Powershell在完成的脚本中添加多线程

[英]Powershell adding multithreading to finished script

我得到了一个脚本,它工作正常,但是我想通过使用多线程来加快速度。 问题是,在不更改大量代码的情况下很难做到这一点。 我希望避免使用具有新的单独作用域的作业,但这似乎是不可能的。 另外,start-job真的很慢,它需要~~ 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
}
# 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
#>

首先,查看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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM