F# Hopac timeOutMillis未按预期工作
我最近开始和霍帕克一起玩,觉得这太棒了。然而,这里有一个我无法理解的问题。下面是代码片段:F# Hopac timeOutMillis未按预期工作,f#,hopac,F#,Hopac,我最近开始和霍帕克一起玩,觉得这太棒了。然而,这里有一个我无法理解的问题。下面是代码片段: let rnd = new Random() let logger (msg:string) = let c = Ch<string>() let msgLoop = job { for i in [1..15] do let msgStr = sprintf "%s %d" msg i do! timeOut
let rnd = new Random()
let logger (msg:string) =
let c = Ch<string>()
let msgLoop = job {
for i in [1..15] do
let msgStr = sprintf "%s %d" msg i
do! timeOutMillis (rnd.Next(1000,3000))
do! c *<- msgStr
}
printfn "Started job %s" msg
msgLoop |> start
c
let timeout = timeOutMillis 3000
let c = logger "Instance Foo"
let rec printLoop () = Job.delay <| fun () ->
Alt.choose[
timeout ^=> fun () -> printfn "job timed out"
Job.result ()
Ch.take c ^=> fun msg -> printfn "Log: %s" msg
printLoop ()
]
printLoop () |> start
让rnd=new Random()
let记录器(消息:字符串)=
设c=Ch()
让msgLoop=job{
因为我在[1..15]中是这样做的
让msgStr=sprintf“%s%d”消息i
do!timeOutMillis(rnd.Next(10003000))
开始
C
让超时=超时毫秒3000
设c=logger“实例Foo”
让rec printLoop()=Job.delay
Alt.选择[
超时^=>fun()->printfn“作业超时”
Job.result()
Ch.take c^=>fun msg->printfn“Log:%s”msg
printLoop()
]
printLoop()|>开始
我认为在3000毫秒后,超时选项将变为可用,并将中止打印消息。这不会发生,只有在通道c
中没有任何内容时才会触发超时
我把timeout放在Alt.choose列表的第一个位置,因为根据文档,如果同时有多个备选方案可用,则会选择列表中第一个出现的方案
()
非常感谢您提供的任何帮助这里是一个快速的答案。该行没有启动超时:
let timeout = timeOutMillis 3000
而是在线路上(重新)启动超时
timeout ^=> fun () -> printfn "job timed out"
每次计算该行时。程序的最短修复方法是更改定义超时的行:
let timeout = timeOutMillis 3000 |> memo
非常感谢,我从来没有想到每次评估超时时都会重新启动。现在它就像一个符咒