F# 为什么此F代码与MailboxProcessor一起使用时不会生成预期输出?

F# 为什么此F代码与MailboxProcessor一起使用时不会生成预期输出?,f#,asynchronous,agent,mailboxprocessor,F#,Asynchronous,Agent,Mailboxprocessor,我正在浏览Don Syme的一篇博客文章。然而,下面看似极其简单的代码并没有像预期的那样生成输出 type Agent<'T> = MailboxProcessor<'T> let agent = Agent.Start(fun inbox -> async { while true do let! msg = inbox.Receive() printfn "got message '%

我正在浏览Don Syme的一篇博客文章。然而,下面看似极其简单的代码并没有像预期的那样生成输出

type Agent<'T> = MailboxProcessor<'T>

let agent =
   Agent.Start(fun inbox ->
     async { while true do
               let! msg = inbox.Receive()
               printfn "got message '%s'" msg } )

for i in 1 .. 10000 do
   agent.Post (sprintf "message %d" i)
我在Ubuntu下使用Mono 2.8.1只收到了大约3000条消息,在Windows XP下使用Visual F只收到了15条消息,而不是预期的10000条消息。我有什么遗漏吗?顺便说一句,我试图用以下文件op替换printfn语句,结果是相同的部分结果

open System.IO
type Agent<'T> = MailboxProcessor<'T>

let agent =
   Agent.Start(fun inbox ->
     async { while true do
               let! msg = inbox.Receive()
               use logger = new StreamWriter("a.log", true)
               logger.WriteLine("got message '{0}'", msg.ToString())
               logger.Close() 
           } )

for i in 1 .. 10000 do
   agent.Post (sprintf "message %d" i)

只需在Win机器中运行您的代码-一切正常。尝试添加

ignore( System.Console.ReadKey() )

最后一行,因为agent.Post是非阻塞的,在发布10000条消息后,控制流将向前移动,可能会退出程序

我刚刚用Mono JIT编译器版本2.8.1 tarball上的F Interactive验证了这一点,周一11月22日09:52:37 MST 2010 MacBook。只收到了3000条左右的信息,它们正常吗?2000年、2001年、2002年等等?有跳绳吗?跳跃是周期性的吗?嗨,克里斯,它们是有序的,没有跳跃。我没有在FSI中运行代码,这似乎是原因。也许这就是Don期望读者运行代码的方式。只是根据desco的提示解决了问题。是的,就是这样。谢谢你的快速回复。