gpt4 book ai didi

asynchronous - f# 在 list.iteri 中执行 await 异步方法

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

我在 F# 4.0 中有以下代码

let processEscalation escalationAction (escalationEvents:UpdateCmd.Record list) =
printf "%A" Environment.NewLine
printf "Started %A" escalationAction
escalationEvents
|> List.iter ( fun x ->
printf "%A" Environment.NewLine
printf "escalation %A for with action: %A" x.incident_id escalationAction
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
|> Async.AwaitTask
|> ignore)


let ComposeEscalation() =
let escalationlevels = ["ESC1 REACHED"; "ESC2 REACHED"; "ESC3 REACHED"]
escalationlevels
|> List.map getEscalationEvents
|> List.iteri (fun i x -> processEscalation escalationlevels.[i] x)

其中以下行是对返回 Task 的 C# 异步方法的调用
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))

compose escalation 方法调用 processEscalation 3 次。
但是,第二个调用在第一个调用完成之前开始。
如何确保最后一行 list.iteri 等待并按顺序处理它们?
也许 processEscalation 应该在异步计算表达式中?

最佳答案

什么 Async.AwaitTask是它返回一个 Async可用于等待任务完成的计算。在你的情况下,你从来没有对它做任何事情,所以循环只会继续下一次迭代。

你想要这样的东西:

service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated")) 
|> Async.AwaitTask
|> Async.RunSynchronously
|> ignore

这应该会产生您期望的效果,尽管肯定有更好、更可组合的方式来表达这种逻辑。

编辑:我的意思是这样的,核心的对应物 Async.Parallel功能:
module Async = 

let sequential (asyncs: seq<Async<'t>>) : Async<'t []> =
let rec loop acc remaining =
async {
match remaining with
| [] -> return Array.ofList (List.rev acc)
| x::xs ->
let! res = x
return! loop (res::acc) xs
}
loop [] (List.ofSeq asyncs)

然后你可以按照以下方式做一些事情:
escalationEvents
// a collection of asyncs - note that the task won't start until the async is ran
|> List.map (fun x ->
async {
let task =
service.PostAction(new Models.Action(x.incident_id, escalationAction, "escalated"))
return! Async.AwaitTask task
})
// go from a collection of asyncs into an async of a collection
|> Async.sequential
// you don't care about the result, so ignore it
|> Async.Ignore
// now that you have your async, you need to run it in a way that makes sense
// in your context - Async.Start could be another option.
|> Async.RunSynchronously

这里的好处是,您不是将所有内容都捆绑到一个循环中,而是将计算拆分为明确界定的阶段。它易于遵循和重构(例如,如果您需要并行处理这些事件,则只需在管道中切换一步)。

关于asynchronous - f# 在 list.iteri 中执行 await 异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395198/

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