Cyg*_*n98 2 f# asynchronous agent mailboxprocessor
我正在浏览Don Syme的一篇博客文章,其中包括F#:Agents中的Async和Parallel Design Patterns.但是,以下看似非常简单的代码没有按预期生成输出.
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)
Run Code Online (Sandbox Code Playgroud)
而不是预期的10,000条消息,我只使用Ubuntu下的Mono 2.8.1获得大约3000条消息,或者在Windows XP下使用Visual F#获得15条消息.我在这里错过了什么吗?顺便说一句,我试图用以下文件操作替换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)
Run Code Online (Sandbox Code Playgroud)
只需在Win机器中运行您的代码 - 一切正常.尝试添加
ignore( System.Console.ReadKey() )
Run Code Online (Sandbox Code Playgroud)
作为最后一行,因为agent.Post是非阻塞的,并且在发布10000个消息之后,控制流将向前移动,可能退出程序.
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |