繁体   English   中英

F# 邮箱处理器问题

[英]F# MailboxProcessor questions

我使用http://fssnip.net/3K中的代码创建了一个控制台程序。 我发现

  1. 我要在末尾添加“System.Console.ReadLine() |> ignore”以等待线程完成。 是否可以告诉所有 MailBoxProcessor 都已完成并且程序可以自行退出?

  2. 我试图将测试 url "www.google.com" 更改为无效的 url 并得到以下 output。 能否避免“输出竞赛”?

     http://www.google.co1m crawled by agent 1.  
     AgAAAent gent 3 is done.  
     gent 2 is done.  
     5 is done.  
     gent 4 is done.  
     Agent USupervisor RL collector is done.  
     is done.  
     1 is done.

[编辑]

使用 Tomas 的更新http://fssnip.net/65后,最后的输出/爬取仍然终止。 以下是我将“limit”改为5并添加一些调试信息后程序的output。 最后一行显示截断的 URL。 这是一种检测所有爬虫是否完成执行的方法吗?

[Main] before crawl
[Crawl] before return result
http://news.google.com crawled by agent 1.
[supervisor] reached limit
http://www.gstatic.com/news/img/favicon.ico crawled by agent 5.
Agent 2 is done.
[supervisor] reached limit
Agent 5 is done.
http://www.google.com/imghp?hl=en&tab=ni crawled by agent 3.
[supervisor] reached limit
Agent 3 is done.
http://www.google.com/webhp?hl=en&tab=nw crawled by agent 4.
[supervisor] reached limit
Agent 4 is done.
http://news.google.com/n

我将主要代码更改为

printfn "[Main] before crawl"
crawl "http://news.google.com" 5
|> Async.RunSynchronously
printfn "[Main] after crawl"

但是,最后一个printfn "[Main] after crawl"永远不会执行,除非我在最后添加一个 Console.Readline() 。

[编辑 2]

代码在 fsi 下运行良好。 但是,如果使用 fsi --use:Program.fs --exec --quiet 运行它也会有同样的问题

我创建了一个片段,它使用您询问的两个功能扩展了前一个片段: http://fssnip.net/65

  1. 为了解决这个问题,我添加了带有AsyncReplyChannel<unit>Start消息。 当主管代理启动时,它会等待此消息并保存回复通道以供以后使用。 完成后,它使用此通道发送回复。

    启动代理的 function 返回等待回复的异步工作流。 然后,您可以使用Async.RunSynchronously调用crawl ,这将在主管代理完成时完成。

  2. 为避免打印时出现竞争,您需要同步所有打印。 最简单的方法是编写一个新代理:-)。 代理接收字符串并一一打印到output(使它们不能交错)。 该代码段隐藏了标准printfn function,其中包含向代理发送字符串的新实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM