为什么我的邮箱处理器挂起了?

时间:2012-05-29 08:33:24

标签: f# mailboxprocessor

我无法弄清楚为什么在调用GetTotal时挂起以下代码。我似乎无法在MailboxProcessor内部进行调试,因此很难看到发生了什么。

module Aggregator

open System

type Message<'T, 'TState> =
    | Aggregate of 'T
    | GetTotal of AsyncReplyChannel<'TState>

type Aggregator<'T, 'TState>(initialState, f) =
    let myAgent = new MailboxProcessor<Message<'T, 'TState>>(fun inbox ->
        let rec loop agg =
            async {
                let! message = inbox.Receive()
                match message with
                    | Aggregate x -> return! loop (f agg x)
                    | GetTotal replyChannel ->
                        replyChannel.Reply(agg)
                        return! loop agg
            }
        loop initialState
        )

    member m.Aggregate x = myAgent.Post(Aggregate(x))
    member m.GetTotal = myAgent.PostAndReply(fun replyChannel -> GetTotal(replyChannel))

let myAggregator = new Aggregator<int, int>(0, (+))

myAggregator.Aggregate(3)
myAggregator.Aggregate(4)
myAggregator.Aggregate(5)

let totalSoFar = myAggregator.GetTotal
printfn "%d" totalSoFar

Console.ReadLine() |> ignore

直接使用相同的MailboxProcessor似乎工作正常,而不是包装在Aggregator类中。

2 个答案:

答案 0 :(得分:7)

问题是您没有启动代理。您可以在创建代理后调用Start

let myAgent = (...)
do myAgent.Start()

或者,您可以使用MailboxProcessor<'T>.Start创建代理而不是调用构造函数(我通常更喜欢这个选项,因为它看起来更实用):

let myAgent = MailboxProcessor<Message<'T, 'TState>>.Start(fun inbox ->  (...) )

我认为您无法调试代理,因为代理内部的代码实际上并未运行。我尝试在代理内部printfn "Msg: %A" message调用后立即添加Receive(打印传入的消息以进行调试),我注意到,在调用Aggregate后,代理实际上没有收到任何消息...(仅在致电GetTotal后才会被阻止,这有效回复)

作为旁注,我可能会将GetTotal转换为方法,因此您需要调用GetTotal()。每次访问属性时都会重新评估属性,因此您的代码执行相同的操作,但最佳做法不建议使用执行复杂工作的属性。

答案 1 :(得分:4)

您忘了启动邮箱:

open System

type Message<'T, 'TState> =
    | Aggregate of 'T
    | GetTotal of AsyncReplyChannel<'TState>

type Aggregator<'T, 'TState>(initialState, f) =
    let myAgent = new MailboxProcessor<Message<'T, 'TState>>(fun inbox ->
        let rec loop agg =
            async {
                let! message = inbox.Receive()
                match message with
                    | Aggregate x -> return! loop (f agg x)
                    | GetTotal replyChannel ->
                        replyChannel.Reply(agg)
                        return! loop agg
            }
        loop initialState
        )

    member m.Aggregate x = myAgent.Post(Aggregate(x))
    member m.GetTotal = myAgent.PostAndReply(fun replyChannel -> GetTotal(replyChannel))
    member m.Start() = myAgent.Start()

let myAggregator = new Aggregator<int, int>(0, (+))

myAggregator.Start()

myAggregator.Aggregate(3)
myAggregator.Aggregate(4)
myAggregator.Aggregate(5)

let totalSoFar = myAggregator.GetTotal
printfn "%d" totalSoFar

Console.ReadLine() |> ignore