Hopac timeOutMillis 未按预期工作
Hopac timeOutMillis does not work as expected
我最近开始玩 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! 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
我假设在 3000 毫秒过去后,超时选项将变得可用,并将中止打印消息。这并没有发生,只有当通道 c
中没有任何内容时才会触发超时。
我将超时放在 Alt.choose 列表中的第一位,因为根据文档,如果同时有多个备选方案可用,则选择列表中第一个出现的备选方案
(On the Semantics of Alternatives)
非常感谢任何帮助
这是一个快速的答案。超时未启动上线:
let timeout = timeOutMillis 3000
而是超时(重新)开始上线
timeout ^=> fun () -> printfn "job timed out"
每次对行进行求值。该程序的最短修复方法是将您定义超时的行更改为:
let timeout = timeOutMillis 3000 |> memo
我最近开始玩 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! 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
我假设在 3000 毫秒过去后,超时选项将变得可用,并将中止打印消息。这并没有发生,只有当通道 c
中没有任何内容时才会触发超时。
我将超时放在 Alt.choose 列表中的第一位,因为根据文档,如果同时有多个备选方案可用,则选择列表中第一个出现的备选方案 (On the Semantics of Alternatives)
非常感谢任何帮助
这是一个快速的答案。超时未启动上线:
let timeout = timeOutMillis 3000
而是超时(重新)开始上线
timeout ^=> fun () -> printfn "job timed out"
每次对行进行求值。该程序的最短修复方法是将您定义超时的行更改为:
let timeout = timeOutMillis 3000 |> memo