[英]mailboxprocessor does not terminate
我在消息循環中有一個超時異常(我真的打算設置一個超時),我試圖按如下方式捕獲它
let printerAgent = MailboxProcessor.Start(fun inbox->
// the message processing function
let rec messageLoop() = async{
try
// read a message
let! msg = inbox.Receive 30000
// process a message
match msg with
| Message text ->
sw.WriteLine("{0}: {1}", DateTime.UtcNow.ToShortTimeString(), text)
printfn "%s" text
// loop to top
return! messageLoop()
| Shutdown replyChannel ->
replyChannel.Reply()
// We do NOT do return! messageLoop() here
with
| exc ->
printfn "%s" exc.Message
}
// start the loop
messageLoop()
)
我可以看到控制台中打印的超時消息,但程序永遠不會結束:我錯過了什么?
這就是我在代碼中調用printerAgent
的方式
printerAgent.PostAndReply( (fun replyChannel -> Shutdown replyChannel), 10000)
請注意,使用inbox.Receive()
它最終會在幾分鍾后正常終止,但我的目標是設置超時(例如 30 秒)。
我想我看到了概念上的問題。 在收到最終關閉消息之前,我無法終止消息循環(否則程序發送的所有以下printerAgent.Post
消息將在隊列中保持未處理狀態,不會因任何錯誤而阻塞程序和最終關閉消息,由打印機代理發送的printerAgent.PostAndReply
將超時,也不會因任何錯誤而阻塞程序)。 我應該返回消息循環,以便它在超時后實際上可以繼續,就像正常接收到的消息一樣:
with
| exc ->
printfn "%s" exc.Message
return! messageLoop() // important! I guess I can't really terminate the message loop from here
此時程序同時終止:我只看到控制台中打印了很多Timeout of Mailbox.Receive
(在消息循環空閑等待接收消息時每 n=30 秒,因此它們可以提供信息關於經過的時間)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.