5

我试图找到一个有关如何使用的示例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
)

对评论的反应:与 otherMailboxProcessor或并行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
4

3 回答 3

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消息的速度超过了处理它们的速度,您就会遇到麻烦。例如,当您已经在处理前一个请求时,可以通过忽略或排队请求来解决此问题。

于 2011-02-02T22:27:50.363 回答
3

不要使用 TRYSCAN !!!

不幸的是,TryScan当前版本的 F# 中的函数在两个方面被破坏了。首先,重点是指定超时,但实现实际上并没有兑现它。具体来说,不相关的消息会重置计时器。其次,与其他Scan函数一样,消息队列在一个锁定下进行检查,该锁定防止任何其他线程在扫描期间发布,这可以是任意长的时间。因此,TryScan函数本身往往会锁定并发系统,甚至会引入死锁,因为调用者的代码是在锁内计算的(例如,ScanTryScan锁下的代码阻塞等待获取锁定它已经在下面)。

TryScan在我的生产代码的早期原型中使用过,它导致了无穷无尽的问题。然而,我设法围绕它进行架构设计,最终的架构实际上更好。本质上,我急切地Receive使用我自己的本地队列来过滤所有消息和过滤器。

于 2011-02-03T21:26:10.357 回答
2

正如 Tomas 提到的 MailboxProcessor 是单线程的。您将需要另一个 MailboxProcessor 在与状态获取器不同的线程上运行更新。

#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

你也可以使用线程池。

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() |> 忽略

于 2011-02-03T00:59:13.837 回答