小编Che*_*Max的帖子

为什么 Async.Sequential 不能按预期工作(但在 FSI 中工作)?

open System
open System.Net

let fetchUrlAsync url = 
    async {
        Console.WriteLine(sprintf "Fetch <%s>" url)
        let req = WebRequest.Create(Uri(url)) 
        use! resp = req.AsyncGetResponse()   
        use stream = resp.GetResponseStream() 
        use reader = new IO.StreamReader(stream) 
        let html = reader.ReadToEnd() 
        Console.WriteLine(sprintf "finished downloading %s. Length = %i" url html.Length)
    }

[<EntryPoint>]
let main argv =
    ["http://bing.com"; "http://ya.ru"; "http://google.com"]
    |> List.map fetchUrlAsync
    |> Async.Sequential
    |> Async.Ignore
    |> Async.RunSynchronously


Run Code Online (Sandbox Code Playgroud)

输出:

Fetch <http://bing.com>
Fetch <http://ya.ru>
Fetch <http://google.com>
finished downloading http://google.com. Length = 50592
finished downloading http://ya.ru. Length = …
Run Code Online (Sandbox Code Playgroud)

f# asynchronous f#-interactive

4
推荐指数
1
解决办法
98
查看次数

标签 统计

asynchronous ×1

f# ×1

f#-interactive ×1