背景
我正在试着找出邮箱处理器。其想法是将其用作某种状态机,在状态之间传递参数,然后退出。有些部分会有异步通信,所以我在那里做了一个睡眠。这是一个控制台应用程序,制作一个Post什么都不做,因为主线程退出并杀死了它后面的所有东西。此外,我尝试过不使用
let sleepWorkflow = async
,没有任何区别。
问题
(我可能做错了什么)
Go24不是异步的。将RunSynchronously更改为StartImmediate没有明显区别。结尾应该在GetMe下面的某个地方。同时,在提取之后打印"完成"。控件不是应该在睡眠时返回到主线程吗?
Go24,等等go24 1,结束提取1完成GetMe…
运行时间非常慢。Fetch没有延迟,大约是10秒(秒表)。我认为F#线程是轻量级的,应该使用线程池。根据调试器的说法,创建每个线程大约需要1秒,看起来像真正的线程。
此外,根据ProcessExplorer的说法,更改为[1..100]将使程序"暂停"100秒,在此期间创建100个线程,然后打印所有内容。实际上,我更喜欢更少的线程和缓慢的增长。
代码。
程序.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
邮箱.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 )
我不一定确定这是主要问题,但代理代码中的嵌套异步和Async.RunSynchrously
看起来很可疑。
您不需要创建嵌套的async,只需直接调用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()