Parallel processing 异步函数并行调用

Parallel processing 异步函数并行调用,parallel-processing,f#,Parallel Processing,F#,以下版本正在同步调用所有函数 我想了解如何并行调用异步函数,并将所有结果和错误返回给调用者 请求 let requestAsync (url: string) : Async<Result<string, Error>> = async { Console.WriteLine ("Simulating request " + url) try do! Async.Sleep(1000)

以下版本正在同步调用所有函数

我想了解如何并行调用异步函数,并将所有
结果
错误
返回给调用者

请求

let requestAsync (url: string) : Async<Result<string, Error>> =
    async {
        Console.WriteLine ("Simulating request " + url)
        try
            do! Async.Sleep(1000)
            return Ok (url + ": body...")
        with :? WebException as e ->
            return Error {code = 500; message = "Internal Server Error";}
    }
[<TestMethod>]
member this.TestrequestAsync() =
    let urls = [|
        "http://www.example.com/1";
        "http://www.example.com/2";
        "http://www.example.com/3";
        "http://www.example.com/4";
        "http://www.example.com/5";
        "http://www.example.com/6";
        "http://www.example.com/7";
        "http://www.example.com/8";
        "http://www.example.com/9";
        "http://www.example.com/10";
    |]

    urls
    |> Array.map (fun url -> requestAsync url |> Async.RunSynchronously) // Async.Parallel some mismatch

    // Iterate results
result |> Array.iter (fun data -> match data with
                                      | Ok result -> Console.WriteLine(result)
                                      | Error error -> Console.WriteLine(error) )
尝试

let requestAsync (url: string) : Async<Result<string, Error>> =
    async {
        Console.WriteLine ("Simulating request " + url)
        try
            do! Async.Sleep(1000)
            return Ok (url + ": body...")
        with :? WebException as e ->
            return Error {code = 500; message = "Internal Server Error";}
    }
[<TestMethod>]
member this.TestrequestAsync() =
    let urls = [|
        "http://www.example.com/1";
        "http://www.example.com/2";
        "http://www.example.com/3";
        "http://www.example.com/4";
        "http://www.example.com/5";
        "http://www.example.com/6";
        "http://www.example.com/7";
        "http://www.example.com/8";
        "http://www.example.com/9";
        "http://www.example.com/10";
    |]

    urls
    |> Array.map (fun url -> requestAsync url |> Async.RunSynchronously) // Async.Parallel some mismatch

    // Iterate results
result |> Array.iter (fun data -> match data with
                                      | Ok result -> Console.WriteLine(result)
                                      | Error error -> Console.WriteLine(error) )
迭代使用For in

for r in result do
    match r with
    | Ok re -> Console.WriteLine(re)
    | Error error -> Console.WriteLine(error)

您可以使用
Async.Parallel
并行运行许多异步操作:

let results =
  urls
  |> Seq.map requestAsync   // seq<Async<'T>>
  |> Async.Parallel         // async<T' []>
  |> Async.RunSynchronously // T' []

您可以使用
Async.Parallel
并行运行许多异步操作:

let results =
  urls
  |> Seq.map requestAsync   // seq<Async<'T>>
  |> Async.Parallel         // async<T' []>
  |> Async.RunSynchronously // T' []

谢谢,你能为迭代添加部分吗,请看我的编辑。如果我要解释答案,你创建了一系列
requestAsync
调用,并并行调用所有调用
Async.parallel
并等待它们完成
Async.RunSynchronously
请看我的尝试,你同意我的尝试吗,还是有一个漂亮优雅的解决方案?@Developer11你迭代打印结果的代码对我来说很好,尽管还有其他方法可以考虑它,例如,你可能会发现。。在里面读得更好。请在edit中查看
,这就是你的意思吗?谢谢,你能为迭代添加部分吗,请参阅我的编辑。如果我要解释答案,您创建了一系列的
requestAsync
调用,并并行调用了所有调用
Async.parallel
并等待它们完成
Async.RunSynchronously
请查看我的尝试,你同意我的尝试吗,还是有一个漂亮优雅的解决方案?@Developer11你迭代打印结果的代码对我来说很好,尽管还有其他方法可以考虑它,例如,你可能会发现。。在里面请检查
中的
编辑,这就是你的意思吗?