F# 继续 Async.Catch

F# Continue on Async.Catch

我正在执行许多异步 Web 请求并使用 Async.Parallel。类似于:

xs                          
|> Seq.map (fun u -> downloadAsync u.Url)
|> Async.Parallel
|> Async.Catch

有些请求可能会抛出异常,我想记录它们并继续处理其余的网址。我找到了 Async.Catch 函数,但这会在抛出第一个异常时停止计算。我 知道 我可以在异步表达式中使用 try...with 表达式来计算整个列表,但是,我认为,这意味着将日志函数传递给我的 downloadAsync 函数改变他的类型。有没有其他方法可以捕获异常,记录它们并继续使用其余的网址?

'trick' 是将 catch 移动到 map 中,以便 catch 也被并行化:

open System
open System.IO
open System.Net

type T = { Url : string }

let xs = [
    { Url = "http://microsoft.com" }
    { Url = "thisDoesNotExists" } // throws when constructing Uri, before downloading
    { Url = "https://thisDotNotExist.Either" }
    { Url = "http://google.com" }
]

let isAllowedInFileName c =
    not <| Seq.contains c (Path.GetInvalidFileNameChars())

let downloadAsync url =
    async {
        use client = new WebClient()
        let fn =
            [|
                __SOURCE_DIRECTORY__
                url |> Seq.filter isAllowedInFileName |> String.Concat
            |]
            |> Path.Combine
        printfn "Downloading %s to %s" url fn
        return! client.AsyncDownloadFile(Uri(url), fn)
    }

xs
|> Seq.map (fun u -> downloadAsync u.Url |> Async.Catch)
|> Async.Parallel
|> Async.RunSynchronously
|> Seq.iter (function
    | Choice1Of2 () -> printfn "Succeeded"
    | Choice2Of2 exn -> printfn "Failed with %s" exn.Message)

(*
Downloading http://microsoft.com to httpmicrosoft.com
Downloading thisDoesNotExists to thisDoesNotExists
Downloading http://google.com to httpgoogle.com
Downloading https://thisDotNotExist.Either to httpsthisDotNotExist.Either
Succeeded
Failed with Invalid URI: The format of the URI could not be determined.
Failed with The remote name could not be resolved: 'thisdotnotexist.either'
Succeeded
*)

这里我把下载打包成另一个async来捕获Uri构造异常