我将使用powershell访问一些网址,我想加快速度,我使用RunspacePool,请参阅以下代码,
$throttleLimit = 100
$SessionState = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$Pool = [runspacefactory]::CreateRunspacePool(1, $throttleLimit, $SessionState, $Host)
$Pool.Open()
$ScriptBlock = {
param($id)
Start-Sleep -Seconds 2
$resp = Invoke-RestMethod "http://google.com" # some kind of url
write-host "Done processing ID $id"
}
$threads = @()
$handles = for ($x = 1; $x -le 100; $x++) {
$powershell = [powershell]::Create().AddScript($ScriptBlock).AddArgument($x)
$powershell.RunspacePool = $Pool
$powershell.BeginInvoke()
$threads += $powershell
}
do {
$i = 0
$done = $true
foreach ($handle in $handles) {
if ($handle -ne $null) {
if ($handle.IsCompleted) {
$threads[$i].EndInvoke($handle)
$threads[$i].Dispose()
$handles[$i] = $null
} else {
$done = $false
}
}
$i++
}
if (-not $done) { Start-Sleep -Milliseconds 500 }
} until ($done)
我的问题是:是否有最大连接大小的配置,因为我将池的已打开运行空间的最大数量设置为100,并且我在该池中创建了100个线程,但它似乎并非并行运行,他们需要大约15秒才能访问google.com