I am using an agent (MailboxProcessor) to perform processing using a state where a response is required.
- The caller sends a message using
MailboxProcessor.PostAndAsyncReply - Inside the agent, the response is provided using
AsyncReplyChannel.Reply
However, I found, digging out the source code f #, that the agent would not process the next message until the response was sent. This is good overall. But in my case, it is more desirable for the agent to continue to process messages than to wait for a response to be delivered.
Is it problematic to do something like this to deliver a response? (Or is there a better alternative?)
async { replyChannel.Reply response } |> Async.Start
I understand that this method does not guarantee that the answers will be delivered in order. I'm fine 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)) ... }
source share