我有以下代码:
//Send request and get response
let req = WebRequest.Create(Uri("https://www.google.com/))
req.Proxy <- null
req.Method <- WebRequestMethods.Http.Get
use resp = req.GetResponse()它大约需要250毫秒来获得请求,这可能看起来并不是那么多。但我将检查大约200个链接,所以我真的希望这个时间减少。有没有办法加快速度呢?
发布于 2019-01-21 00:57:10
您可以使用F#异步工作流来实现这一点,并使用Async.Parallel并行化您的请求。您还需要设置允许的并行连接数量的.NET限制,以便实际允许更高级别的并行度。如下所示:
open System
open System.Net
ServicePointManager.DefaultConnectionLimit <- 20
let checkUrl url =
async {
let req = WebRequest.Create(Uri(url))
req.Proxy <- null
req.Method <- WebRequestMethods.Http.Get
use! resp = req.AsyncGetResponse()
printfn "Downloaded: %s" url
return resp.ContentLength }
[ for i in 0 .. 100 -> checkUrl (sprintf "http://www.google.com?%d" i) ]
|> Async.Parallel
|> Async.RunSynchronouslyhttps://stackoverflow.com/questions/54270294
复制相似问题