macos – 为什么我不使用这个F#代码在OSX上输出,但我在Windows上
发布时间:2020-12-14 05:32:07 所属栏目:Windows 来源:网络整理
导读:我正在尝试使用Visual Studio代码中的FSI和ionide插件在我的macbook pro上执行以下F#脚本代码. #r "packages/Newtonsoft.Json.9.0.1/lib/net40/Newtonsoft.Json.dll"#r "System.Net.Http"open Systemopen System.Net.Httpopen Newtonsoft.Jsonlet client = n
我正在尝试使用Visual Studio代码中的FSI和ionide插件在我的macbook pro上执行以下F#脚本代码.
#r "packages/Newtonsoft.Json.9.0.1/lib/net40/Newtonsoft.Json.dll" #r "System.Net.Http" open System open System.Net.Http open Newtonsoft.Json let client = new HttpClient() type AlbumInfo = { userId:int; id:int; title:string } let url = "https://jsonplaceholder.typicode.com/albums/1" async { let! res = Async.AwaitTask <| client.GetAsync(url) let! content = Async.AwaitTask <| res.Content.ReadAsStringAsync() let x = JsonConvert.DeserializeObject<AlbumInfo>(content) printfn "%s" x.title } |> Async.Start printfn "Please wait..." 但我没有得到任何输出请等待….但是,当我把https://jsonplaceholder.typicode.com/albums/1放入浏览器时,我得到了预期的Json响应.所以我知道到达API是没有问题的. 此外,当我在Windows 10 PC上的Visual Studio 2013中运行相同的代码时.代码产生预期的结果.即请等待……以及专辑的标题. 任何想法为什么它在我的macbook上无法正常工作? 解决方法
在Visual Studio中,有一个托管FSI的进程,并保持异步计算的线程(池)保持活动状态.在命令行或VS代码的FSI中,FSI将在主线程完成写入后立即终止请等待…(通常是在线程池上开始计算之前).
如果要观察异步计算的副作用,则必须等待其结果(在此示例单元中): let computation = async { printfn "Starting async" let! res = Async.AwaitTask <| client.GetAsync(url) let! content = Async.AwaitTask <| res.Content.ReadAsStringAsync() let x = JsonConvert.DeserializeObject<AlbumInfo>(content) printfn "Downloaded %s" x.title } async { let! started = computation |> Async.StartChild let! _ = Async.Sleep 1 // only here to get interleaved ouput printfn "Please wait..." let! res = started printfn "Got result %A" res } |> Async.RunSynchronously 可能打印:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows-server-2003 – 将Windows 7 64位打印驱动程序添加
- MVVM Windows Phone 8 – 向地图添加图钉集合
- Windows – 20Mbps WAN通过IPSec隧道限制为10Mbps
- windows-server-2012 – 我的180天服务器2012评估的10天宽限
- winapi – 如何在Vista的Common Item Dialog中添加可编辑的
- windows 2003服务器定时重启的设置方法
- Windows – Samba无法在没有Internet连接的LAN上工作
- windows – 在焦点外接受输入
- 在Windows上为Ant设置Java路径
- windows – 如何使用LaTeX编写路径?