Asynchronous 为什么Async.Sequential不';不按预期工作(但在FSI工作)? 开放系统 开放系统.Net 让fetchUrlAsync url= 异步的{ Console.WriteLine(sprintf“Fetch”url) 让req=WebRequest.Create(Uri(url)) 使用!resp=req.AsyncGetResponse() 使用stream=resp.GetResponseStream() 使用读卡器=新IO.StreamReader(流) 设html=reader.ReadToEnd() Console.WriteLine(sprintf“已完成下载%s.Length=%i”url html.Length) } [] 让主argv= ["http://bing.com"; "http://ya.ru"; "http://google.com"] |>List.map fetchUrlAsync |>异步顺序 |>异步。忽略 |>异步运行
输出:Asynchronous 为什么Async.Sequential不';不按预期工作(但在FSI工作)? 开放系统 开放系统.Net 让fetchUrlAsync url= 异步的{ Console.WriteLine(sprintf“Fetch”url) 让req=WebRequest.Create(Uri(url)) 使用!resp=req.AsyncGetResponse() 使用stream=resp.GetResponseStream() 使用读卡器=新IO.StreamReader(流) 设html=reader.ReadToEnd() Console.WriteLine(sprintf“已完成下载%s.Length=%i”url html.Length) } [] 让主argv= ["http://bing.com"; "http://ya.ru"; "http://google.com"] |>List.map fetchUrlAsync |>异步顺序 |>异步。忽略 |>异步运行,asynchronous,f#,f#-interactive,Asynchronous,F#,F# Interactive,输出: 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.Get
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
Fetch
取来
取来
下载完毕http://google.com. 长度=50592
下载完毕http://ya.ru. 长度=20541
下载完毕http://bing.com. 长度=81386
我不期望有这样的结果(但也许我的期望是错误的)。但是,如果我在F#interactive中运行相同的代码,则输出是(正如我所预期的):
Fetch
下载完毕http://bing.com. 长度=81386
取来
下载完毕http://ya.ru. 长度=20544
取来
下载完毕http://google.com. 长度=50561
为什么代码在从Rider(控制台应用程序)和F#Interactive运行时表现不同?如果第一个输出是正确的,那么Async.Sequential和Async.Parallel之间有什么区别?如果第一个输出不正确,那么如何修复此问题?目前,异步.Sequential的实现方式如下:
Fetch <http://bing.com>
finished downloading http://bing.com. Length = 81386
Fetch <http://ya.ru>
finished downloading http://ya.ru. Length = 20544
Fetch <http://google.com>
finished downloading http://google.com. Length = 50561
不过,这应该是按顺序运行的。
一直都是这样。而且一直如此
该修复程序现在只在VS上发布,但尚未在主线FSharp.Core
nuget提要中发布
目前获得修复的唯一方法是通过VS2019 16.4 FSI
这就是为什么您看到它在FSI中正常工作,但在编译后的应用程序中却无法正常工作
解决方案
观察#7956,等待它发货。NuGet上提供了FSharp.Core 4.7.1包。这个问题似乎已经解决了。
Fetch <http://bing.com>
finished downloading http://bing.com. Length = 81386
Fetch <http://ya.ru>
finished downloading http://ya.ru. Length = 20544
Fetch <http://google.com>
finished downloading http://google.com. Length = 50561
static member Sequential computations =
Async.Parallel(computations, maxDegreeOfParallelism=1)