[英]F#, MailboxProcessor and Async running slow?
背景。
我试图找出MailboxProcessor。 想法是将其用作某种状态机,并在状态之间传递参数,然后退出。 有些部分将进行异步通信,所以我在那里睡觉了。 这是一个控制台应用程序,发布Post不会执行任何操作,因为主线程会退出并杀死其背后的所有内容。 我主要是在做一个PostAndReply。 另外,我尝试了
let sleepWorkflow = async
,没有任何区别。
问题。
(我可能做错了)
Go24不是异步的。 将RunSynchronously更改为StartImmediate不会产生明显的区别。 末尾应位于GetMe下方。 同时在提取后打印完成。 难道不应该在睡眠时将控件返回到主线程吗?
Go24,等待go24 1,结束获取1完成GetMe ...
运行时间太慢了。 毫不延迟地,获取时间约为10秒(秒表)。 我认为F#线程是轻量级的,应该使用线程池。 根据调试器,创建每个应用程序都需要大约1秒钟的时间,并且看起来像真实线程。
同样,更改为[1..100]将“暂停”程序100秒钟,根据ProcessExplorer在此期间创建的100个线程,然后才打印所有内容。 实际上,我更希望线程数量少而增长缓慢。
码。
Program.fs
[<EntryPoint>]
let main argv =
let a = Mailbox.MessageBasedCounter.DoGo24 1
let a = Mailbox.MessageBasedCounter.DoFetch 1
let b = Mailbox.MessageBasedCounter.GetMe
let task i = async {
//Mailbox.MessageBasedCounter.DoGo24 1
let a = Mailbox.MessageBasedCounter.DoFetch i
return a
}
let stopWatch = System.Diagnostics.Stopwatch.StartNew()
let x =
[1..10]
|> Seq.map task
|> Async.Parallel
|> Async.RunSynchronously
stopWatch.Stop()
printfn "%f" stopWatch.Elapsed.TotalMilliseconds
printfn "a: %A" a
printfn "b: %A" b
printfn "x: %A" x
0 // return an integer exit code
Mailbox.fs
module Mailbox
#nowarn "40"
type parserMsg =
| Go24 of int
| Done
| Fetch of int * AsyncReplyChannel<string>
| GetMe of AsyncReplyChannel<string>
type MessageBasedCounter () =
/// Create the agent
static let agent = MailboxProcessor.Start(fun inbox ->
// the message processing function
let rec messageLoop() = async{
let! msg = inbox.Receive()
match msg with
| Go24 n ->
let sleepWorkflow = async{
printfn "Go24, wait"
do! Async.Sleep 4000
MessageBasedCounter.DoDone() // POST Done.
printfn "go24 %d, end" n
return! messageLoop()
}
Async.RunSynchronously sleepWorkflow
| Fetch (i, repl) ->
let sync = async{
printfn "Fetch %d" i
do! Async.Sleep 1000
repl.Reply( "Reply Fetch " + i.ToString() ) // Reply to the caller
return! messageLoop()
}
Async.RunSynchronously sync
| GetMe (repl) ->
let sync = async{
printfn "GetMe"
repl.Reply( "GetMe" ) // Reply to the caller
return! messageLoop()
}
Async.RunSynchronously sync
| Done ->
let sync = async{
printfn "Done"
return! messageLoop()
}
Async.RunSynchronously sync
}
// start the loop
messageLoop()
)
// public interface to hide the implementation
static member DoDone () = agent.Post( Done )
static member DoGo24 (i:int) = agent.Post( Go24(i) )
static member DoFetch (i:int) = agent.PostAndReply( fun reply -> Fetch(i, reply) )
static member GetMe = agent.PostAndReply( GetMe )
我不一定要确定这是主要问题,但是代理代码中嵌套的asyncs和Async.RunSynchrously
看起来很可疑。
您无需创建嵌套的异步-您可以直接在match
子句的主体中调用异步操作:
// the message processing function
let rec messageLoop() = async{
let! msg = inbox.Receive()
match msg with
| Go24 n ->
printfn "Go24, wait"
do! Async.Sleep 4000
MessageBasedCounter.DoDone()
printfn "go24 %d, end" n
return! messageLoop()
| Fetch (i, repl) ->
(...)
除此之外,重要的是要了解该代理恰好具有运行主体计算的一个实例。 因此,如果您阻止该代理的主体,则所有其他操作都将排队。
如果要在后台启动某些任务(例如同步操作)并立即恢复代理,则可以在主体内部使用Async.Start
(但请确保在主体的主体部分递归调用主循环):
| Go24 n ->
// Create work item that will run in the background
let work = async {
printfn "Go24, wait"
do! Async.Sleep 4000
MessageBasedCounter.DoDone()
printfn "go24 %d, end" n }
// Queue the work in a thread pool to be processed
Async.Start(work)
// Continue the message loop, waiting for other messages
return! messageLoop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.