2
我不明白Async.RunSynchronously
的語義,當給出超時參數。爲什麼下面的例子沒有終止?運行異步計算超時
let runInMaxTime (time: int) (work: Async<'T>) =
try
Async.RunSynchronously(work,time)
|> Some
with
| _ ->
None
let rec forever() : unit =
printfn "Forever looping"
forever()
// Never terminates
let x = runInMaxTime 10 <| async {return forever()}
runInMaxTime
的正確實施是什麼樣的?我唯一想出來的作品是基於Task
s和取消標記。
我明白了,謝謝澄清! – esevelos
@esevelos如果你有一個長期運行的'async'計算,你可以創建一個'nop'函數並在你的代碼中方便的取消點調用它,例如[我可以顯式檢查取消/終止異步計算嗎?]( http://stackoverflow.com/questions/18676657/can-i-explicitly-check-for-cancellation-terminate-async-computation/18677531#18677531) –