I'm using an agent (MailboxProcessor) to do some stateful processing where a response is needed.
- The caller posts a message using
MailboxProcessor.PostAndAsyncReply
- Within the agent, a response is given with
AsyncReplyChannel.Reply
However, I've discovered by poking around the f# source code that the agent will not process the next message until the the response is delivered. This is a good thing in general. But in my case, it is more desirable for the agent to keep processing messages than to wait for response delivery.
Is it problematic to do something like this to deliver the response? (Or is there a better alternative?)
async { replyChannel.Reply response } |> Async.Start
I realize that this method does not guarantee that responses will be delivered in order. I'm okay with that.
Reference example
// agent code
let doWork data =
async { ... ; return response }
let rec loop ( inbox : MailboxProcessor<_> ) =
async {
let! msg = inbox.Receive()
match msg with
| None ->
return ()
| Some ( data, replyChannel ) ->
let! response = doWork data
replyChannel.Reply response (* waits for delivery, vs below *)
// async { replyChannel.Reply response } |> Async.Start
return! loop inbox
}
let agent =
MailboxProcessor.Start(loop)
// caller code
async {
let! response =
agent.PostAndAsyncReply(fun replyChannel -> Some (data, replyChannel))
...
}