2015-07-20 34 views
5

我有幾十萬個需要調用的URL。這些是對應用程序服務器的調用,它將處理它們並將狀態碼寫入表中。我不需要等待響應(成功/失敗),只需要服務器獲得請求。我也希望能夠指定一次可以運行多少個併發作業,因爲我還沒有計算出tomcat可以處理多少個併發請求。如何從列表中異步調用多個URL

這裏就是我這麼遠,基本上是從別人的別人的企圖做類似的事情,只是不能與URL調用拍攝。該文本文件包含各自的行。網址如下:

http://webserver:8080/app/mwo/services/create?server=ServerName&e1user=admin&newMWONum=123456&sourceMWONum=0&tagNum=33-A-1B 

,代碼:

$maxConcurrentJobs = 10 
$content = Get-Content -Path "C:\Temp\urls.txt" 

foreach ($url in $content) { 
    $running = @(Get-Job | Where-Object { $_.State -eq 'Running' }) 
    if ($running.Count -le $maxConcurrentJobs) { 
     Start-Job { 
      Invoke-WebRequest -UseBasicParsing -Uri $using:url 
     } 
    } else { 
     $running | Wait-Job -Any 
    } 
    Get-Job | Receive-Job 
} 

我遇到的一個問題是它給2個錯誤每「工作」,我不知道爲什麼。當我轉儲url數組$ content時,它看起來很好,當我一個接一個地運行我的Invoke-WebRequest時,他們的工作沒有錯誤。

126 Job126   BackgroundJob Running  True   localhost   ...     
Invalid URI: The hostname could not be parsed. 
    + CategoryInfo   : NotSpecified: (:) [Invoke-RestMethod], UriFormatException 
    + FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeRestMethodComman 
    d 
    + PSComputerName  : localhost 

Invalid URI: The hostname could not be parsed. 
    + CategoryInfo   : NotSpecified: (:) [Invoke-RestMethod], UriFormatException 
    + FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeRestMethodComman 
    d 
    + PSComputerName  : localhost 

任何幫助或替代實現將不勝感激。我打算不使用powershell,但我僅限於Windows 7桌面版或Windows 2008 R2服務器,並且我可能會在服務器本身上運行最終腳本,在url中使用localhost來減少網絡延遲。

回答

7

有工作,你承擔了大量的開銷,因爲每個新的工作產生一個新的進程。

使用Runspaces,而不是!

$maxConcurrentJobs = 10 
$content = Get-Content -Path "C:\Temp\urls.txt" 

# Create a runspace pool where $maxConcurrentJobs is the 
# maximum number of runspaces allowed to run concurrently  
$Runspace = [runspacefactory]::CreateRunspacePool(1,$maxConcurrentJobs) 

# Open the runspace pool (very important) 
$Runspace.Open() 

foreach ($url in $content) { 
    # Create a new PowerShell instance and tell it to execute in our runspace pool 
    $ps = [powershell]::Create() 
    $ps.RunspacePool = $Runspace 

    # Attach some code to it 
    [void]$ps.AddCommand("Invoke-WebRequest").AddParameter("UseBasicParsing",$true).AddParameter("Uri",$url) 

    # Begin execution asynchronously (returns immediately) 
    [void]$ps.BeginInvoke() 

    # Give feedback on how far we are 
    Write-Host ("Initiated request for {0}" -f $url) 
} 

正如聯ServerFault文章中指出,你也可以使用一個通用的解決方案,如Invoke-Parallel,它基本上沒有上述

+0

這個完美的作品。謝謝! – trueimage

+0

@ mathias-r-jessen實際上有沒有什麼辦法可以放入異步運行的響應檢查?在這個實現中,併發作業限制確實沒有任何作用,因爲它只是調用所有的url。我需要確保它至少得到了一個代碼200,如果應用程序服務器拒絕連接,它將不會得到處理。 – trueimage

+0

@trueimage我以爲你說過「我不需要等待迴應」:)當你調用異步時,它會使得檢索響應變得有些複雜,但是,它可以完成。 –