【发布时间】:2011-07-02 09:13:13
【问题描述】:
我使用来自http://fssnip.net/3K 的代码创建了一个控制台程序。我发现了
我要在末尾添加“System.Console.ReadLine() |> ignore”以等待线程完成。是否可以告诉所有 MailBoxProcessor 都已完成并且程序可以自行退出?
我尝试将测试 url “www.google.com”更改为无效的 url,并得到以下输出。是否可以避免“输出竞赛”?
[编辑]
在使用 Tomas 的更新 http://fssnip.net/65 后,最后的输出/爬取仍然终止。以下是我将“limit”改为5并添加一些调试信息后程序的输出。最后一行显示截断的 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
【问题讨论】:
-
对我来说,这种使用邮箱处理器来解决 URL 爬取的做法太复杂了,而且 URL 内容的获取也不是异步调用。使用简单的异步计算可以轻松解决该问题。
标签: multithreading asynchronous f# mailboxprocessor