gpt4 book ai didi

asynchronous - 结合 F# 异步函数

转载 作者:行者123 更新时间:2023-12-04 04:08:05 27 4
gpt4 key购买 nike

我在这里有点沮丧。我知道我有所有的东西,但我不知道如何组合它们......

let saveImageToDisk path content =
async {
use s = new FileStream(path, FileMode.OpenOrCreate)
do! s.AsyncWrite(content)
printfn "Done writing %A" path
} // returns Async<unit>

let getImages imageUrls =
imageUrls
|> Seq.map (fun url -> topath url, getImage url)
//Next line not happy because content is Async<byte[]> instead of byte[]
|> Seq.map (fun (path, content) -> saveImageToDisk path content)
|> Async.Parallel
|> Async.RunSynchronously

最佳答案

您可以使用 async 将两者结合起来。表达:

let getImages imageUrls =
imageUrls
|> Seq.map (fun url -> async {
let! content = getImage url
return! saveImageToDisk (topath url) content })
|> Async.Parallel
|> Async.RunSynchronously

关于asynchronous - 结合 F# 异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5693279/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com