[英]F# MailboxProcessor questions
我使用http://fssnip.net/3K中的代碼創建了一個控制台程序。 我發現
我要在末尾添加“System.Console.ReadLine() |> ignore”以等待線程完成。 是否可以告訴所有 MailBoxProcessor 都已完成並且程序可以自行退出?
我試圖將測試 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() 。
代碼在 fsi 下運行良好。 但是,如果使用 fsi --use:Program.fs --exec --quiet 運行它也會有同樣的問題
我創建了一個片段,它使用您詢問的兩個功能擴展了前一個片段: http://fssnip.net/65 。
為了解決這個問題,我添加了帶有AsyncReplyChannel<unit>
的Start
消息。 當主管代理啟動時,它會等待此消息並保存回復通道以供以后使用。 完成后,它使用此通道發送回復。
啟動代理的 function 返回等待回復的異步工作流。 然后,您可以使用Async.RunSynchronously
調用crawl
,這將在主管代理完成時完成。
為避免打印時出現競爭,您需要同步所有打印。 最簡單的方法是編寫一個新代理:-)。 代理接收字符串並一一打印到output(使它們不能交錯)。 該代碼段隱藏了標准printfn
function,其中包含向代理發送字符串的新實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.