Powershell多线程和oracle程序

时间:2016-08-29 01:01:32

标签: multithreading oracle powershell stored-procedures

我的文件列表在1分钟内从未结束增加5。我使用oracle过程将此数据插入数据库。 (方法外部表)

然后我从Powershell调用此程序,如下所示。

ForEach ($file in $files) { 
    #executing the calculate procedure      
    executeStoredProcedure -value sp_load_table_crm -filename $file.Name -conn 
} 

这没关系。但这需要很长时间,并希望加快速度。 哪个使用多线程或多处理更好? 我打算在Windows任务调度程序上设置这个powershell脚本。

1 个答案:

答案 0 :(得分:2)

您可以使用运行空间。试试这个:

$Files = @('File1','File2','File3') #list of your files

$Throttle = 10 #number of threads
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle)
$RunspacePool.Open()

$Jobs = @()
$ScriptBlock = {
    Param($File)
    #place your executeStoredProcedure function inside the script block or make sure it will be loaded with the appropriate module via profile     
    executeStoredProcedure -value sp_load_table_crm -filename ($File.Name) -conn #heres your function and its parameters
}

$Files | % {
    $File = $_
    $Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($File)
    $Job.RunspacePool = $RunspacePool
    $Jobs += New-Object PSObject -Property @{
      Pipe = $Job
      Result = $Job.BeginInvoke()
   }
}

Write-Host "Waiting.." -NoNewline
Do {
   Write-Host "." -NoNewline
   Start-Sleep -Seconds 1
} While ( $Jobs.Result.IsCompleted -contains $false)
Write-Host "All jobs completed!"

$Results = @()
ForEach ($Job in $Jobs)
{   $Results += $Job.Pipe.EndInvoke($Job.Result)
}
$Results | ft -Wrap -AutoSize