我试图找到一个关于如何使用TryScan
的示例,但没找到,你能帮助我吗?
我想做什么(非常简化的例子):我接受了MailboxProcessor
两种类型的消息。
第一个GetState
返回当前状态。
GetState
消息经常发送
另一个UpdateState
非常昂贵(耗时) - 例如从互联网下载内容,然后相应地更新状态。
UpdateState
很少被调用。
我的问题是 - 邮件GetState
被屏蔽,并等到前一个UpdateState
投放。这就是为什么我尝试使用TryScan
处理所有GetState
消息,但没有运气。
我的示例代码:
type Msg = GetState of AsyncReplyChannel<int> | UpdateState
let mbox = MailboxProcessor.Start(fun mbox ->
let rec loop state = async {
// this TryScan doesn't work as expected
// it should process GetState messages and then continue
mbox.TryScan(fun m ->
match m with
| GetState(chnl) ->
printfn "G processing TryScan"
chnl.Reply(state)
Some(async { return! loop state})
| _ -> None
) |> ignore
let! msg = mbox.Receive()
match msg with
| UpdateState ->
printfn "U processing"
// something very time consuming here...
async { do! Async.Sleep(1000) } |> Async.RunSynchronously
return! loop (state+1)
| GetState(chnl) ->
printfn "G processing"
chnl.Reply(state)
return! loop state
}
loop 0
)
[async { for i in 1..10 do
printfn " U"
mbox.Post(UpdateState)
async { do! Async.Sleep(200) } |> Async.RunSynchronously
};
async { // wait some time so that several `UpdateState` messages are fired
async { do! Async.Sleep(500) } |> Async.RunSynchronously
for i in 1..20 do
printfn "G"
printfn "%d" (mbox.PostAndReply(GetState))
}] |> Async.Parallel |> Async.RunSynchronously
如果您尝试运行代码,您会看到GetState
消息几乎没有被处理,因为它等待结果。另一方面,UpdateState
只是一劳永逸,因此阻止了有效的状态。
修改
目前对我有用的解决方案就是这个:
type Msg = GetState of AsyncReplyChannel<int> | UpdateState
let mbox = MailboxProcessor.Start(fun mbox ->
let rec loop state = async {
// this TryScan doesn't work as expected
// it should process GetState messages and then continue
let! res = mbox.TryScan((function
| GetState(chnl) -> Some(async {
chnl.Reply(state)
return state
})
| _ -> None
), 5)
match res with
| None ->
let! msg = mbox.Receive()
match msg with
| UpdateState ->
async { do! Async.Sleep(1000) } |> Async.RunSynchronously
return! loop (state+1)
| _ -> return! loop state
| Some n -> return! loop n
}
loop 0
)
对评论的反应:并行执行MailboxProcessor
的其他ThreadPool
或UpdateState
的想法很棒,但我目前不需要它。
我想做的就是处理所有GetState
个消息,然后处理其他消息。我不关心在处理UpdateState
期间代理是否被阻止。
我会告诉你输出的问题是什么:
// GetState messages are delayed 500 ms - see do! Async.Sleep(500)
// each UpdateState is sent after 200ms
// each GetState is sent immediatelly! (not real example, but illustrates the problem)
U 200ms <-- issue UpdateState
U processing <-- process UpdateState, it takes 1sec, so other
U 200ms 5 requests are sent; sent means, that it is
U 200ms fire-and-forget message - it doesn't wait for any result
and therefore it can send every 200ms one UpdateState message
G <-- first GetState sent, but waiting for reply - so all
previous UpdateState messages have to be processed! = 3 seconds
and AFTER all the UpdateState messages are processed, result
is returned and new GetState can be sent.
U 200ms
U 200ms because each UpdateState takes 1 second
U 200ms
U processing
U
U
U
U
U processing
G processing <-- now first GetState is processed! so late? uh..
U processing <-- takes 1sec
3
G
U processing <-- takes 1sec
U processing <-- takes 1sec
U processing <-- takes 1sec
U processing <-- takes 1sec
U processing <-- takes 1sec
U processing <-- takes 1sec
G processing <-- after MANY seconds, second GetState is processed!
10
G
G processing
// from this line, only GetState are issued and processed, because
// there is no UpdateState message in the queue, neither it is sent
答案 0 :(得分:4)
我不认为TryScan
方法会在这种情况下帮助您。它允许您指定在等待消息时使用的超时。收到某条消息后,它将开始处理该消息(忽略超时)。
例如,如果您想等待某些特定消息,但每秒执行一些其他检查(等待时),您可以写:
let loop () = async {
let! res = mbox.TryScan(function
| ImportantMessage -> Some(async {
// process message
return 0
})
| _ -> None)
match res with
| None ->
// perform some check & continue waiting
return! loop ()
| Some n ->
// ImportantMessage was received and processed
}
在处理UpdateState
消息时,您可以做些什么来避免阻止邮箱处理器?邮箱处理器(逻辑上)是单线程的 - 您可能不希望取消UpdateState
消息的处理,因此最好的选择是在后台开始处理它并等待处理完成。然后,处理UpdateState
的代码可以将一些邮件发送回邮箱(例如UpdateStateCompleted
)。
以下是草图的外观:
let rec loop (state) = async {
let! msg = mbox.Receive()
match msg with
| GetState(repl) ->
repl.Reply(state)
return! scanning state
| UpdateState ->
async {
// complex calculation (runs in parallel)
mbox.Post(UpdateStateCompleted newState) }
|> Async.Start
| UpdateStateCompleted newState ->
// Received new state from background workflow
return! loop newState }
现在后台任务并行运行,您需要注意可变状态。此外,如果您发送UpdateState
消息的速度超过了处理速度,则会遇到麻烦。例如,当您已经处理过前一个请求时,可以通过忽略或排队请求来修复此问题。
答案 1 :(得分:3)
不要使用TRYSCAN !!!
不幸的是,当前版本的F#中的TryScan
函数有两种方式。首先,重点是指定超时,但实现实际上并不尊重它。具体而言,不相关的消息会重置计时器。其次,与其他Scan
函数一样,在锁定下检查消息队列,该锁定阻止任何其他线程在扫描期间发布,这可能是任意长的时间。因此,TryScan
函数本身往往会锁定并发系统,甚至可能引入死锁,因为调用者的代码在锁内部进行评估(例如从函数参数发布到Scan
或TryScan
当锁定块下的代码等待获取它已经存在的锁定时,可以使代理死锁。
我在生产代码的早期原型中使用了TryScan
,并且它没有引起任何问题。但是,我设法围绕它进行构建,结果架构实际上更好。从本质上讲,我急切地Receive
使用我自己的本地队列发送所有消息并进行过滤。
答案 2 :(得分:2)
正如Tomas所说,MailboxProcessor是单线程的。您将需要另一个MailboxProcessor来在状态getter的单独线程上运行更新。
#nowarn "40"
type Msg =
| GetState of AsyncReplyChannel<int>
| UpdateState
let runner_UpdateState = MailboxProcessor.Start(fun mbox ->
let rec loop = async {
let! state = mbox.Receive()
printfn "U start processing %d" !state
// something very time consuming here...
do! Async.Sleep 100
printfn "U done processing %d" !state
state := !state + 1
do! loop
}
loop
)
let mbox = MailboxProcessor.Start(fun mbox ->
// we need a mutiple state if another thread can change it at any time
let state = ref 0
let rec loop = async {
let! msg = mbox.Receive()
match msg with
| UpdateState -> runner_UpdateState.Post state
| GetState chnl -> chnl.Reply !state
return! loop
}
loop)
[
async {
for i in 1..10 do
mbox.Post UpdateState
do! Async.Sleep 200
};
async {
// wait some time so that several `UpdateState` messages are fired
do! Async.Sleep 1000
for i in 1..20 do
printfn "G %d" (mbox.PostAndReply GetState)
do! Async.Sleep 50
}
]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
System.Console.ReadLine() |> ignore
输出:
U start processing 0
U done processing 0
U start processing 1
U done processing 1
U start processing 2
U done processing 2
U start processing 3
U done processing 3
U start processing 4
U done processing 4
G 5
U start processing 5
G 5
U done processing 5
G 5
G 6
U start processing 6
G 6
G 6
U done processing 6
G 7
U start processing 7
G 7
G 7
U done processing 7
G 8
G U start processing 8
8
G 8
U done processing 8
G 9
G 9
U start processing 9
G 9
U done processing 9
G 9
G 10
G 10
G 10
G 10
您也可以使用ThreadPool。
open System.Threading
type Msg =
| GetState of AsyncReplyChannel<int>
| SetState of int
| UpdateState
let mbox = MailboxProcessor.Start(fun mbox ->
let rec loop state = async {
let! msg = mbox.Receive()
match msg with
| UpdateState ->
ThreadPool.QueueUserWorkItem((fun obj ->
let state = obj :?> int
printfn "U start processing %d" state
Async.Sleep 100 |> Async.RunSynchronously
printfn "U done processing %d" state
mbox.Post(SetState(state + 1))
), state)
|> ignore
| GetState chnl ->
chnl.Reply state
| SetState newState ->
return! loop newState
return! loop state
}
loop 0)
[
async {
for i in 1..10 do
mbox.Post UpdateState
do! Async.Sleep 200
};
async {
// wait some time so that several `UpdateState` messages are fired
do! Async.Sleep 1000
for i in 1..20 do
printfn "G %d" (mbox.PostAndReply GetState)
do! Async.Sleep 50
}
]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
System.Console.ReadLine()|&gt;忽略