Che*_*Max 4 f# asynchronous 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.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 = 20541
finished downloading http://bing.com. Length = 81386
Run Code Online (Sandbox Code Playgroud)
我不期望这样的输出(但也许我的期望是错误的)。但是如果我在 F# 交互中运行相同的代码,输出是(如我所料):
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
Run Code Online (Sandbox Code Playgroud)
为什么代码在从 Rider(控制台应用程序)和 F# Interactive 运行时表现不同?如果第一个输出是正确的,那么 Async.Sequential 和 Async.Parallel 之间有什么区别?如果第一个输出不正确,那么如何解决?
目前,Async.Sequential只是实现为:
static member Sequential computations =
Async.Parallel(computations, maxDegreeOfParallelism=1)
Run Code Online (Sandbox Code Playgroud)
尽管RFC 允许这样做,但预计这将按顺序运行。已经发现是个bug。它已由#7596 修复。
该修复程序现在仅在 VS 上提供。但它还不在主线FSharp.Corenuget 提要中。
目前获得修复的唯一方法是通过 VS2019 16.4 FSI。
这就是为什么您会看到它在 FSI 中正常工作,但在您编译的应用程序中却没有。
解决方案
观看#7956,等待发货。
| 归档时间: |
|
| 查看次数: |
98 次 |
| 最近记录: |