MailboxProcessor.PostAndAsyncReply<'Msg,'Reply> 메서드(F#)
메시지를 에이전트에 게시하고 채널에서 비동기적으로 회신을 기다립니다.
네임스페이스/모듈 경로: Microsoft.FSharp.Control
어셈블리: FSharp.Core(FSharp.Core.dll)
// Signature:
member this.PostAndAsyncReply : (AsyncReplyChannel<'Reply> -> 'Msg) * ?int -> Async<'Reply>
// Usage:
mailboxProcessor.PostAndAsyncReply (buildMessage)
mailboxProcessor.PostAndAsyncReply (buildMessage, timeout = timeout)
매개 변수
buildMessage
형식: AsyncReplyChannel<'Reply> -> 'MsgAsyncReplyChannel을 보낼 메시지로 통합하는 함수입니다.
timeout
형식: int회신 메시지를 기다리는 선택적 시간 제한 매개 변수(밀리초)입니다.기본값은 Infinite에 해당하는 -1입니다.
반환 값
에이전트의 회신을 기다리는 비동기 계산(Async 개체)입니다.
설명
메시지는 메시지로 통합되도록 새 회신 채널에 buildMessage를 적용하여 생성됩니다.받는 에이전트는 이 메시지를 처리하고 이 회신 채널에서 Reply 메서드를 정확히 한 번만 호출해야 합니다.
예제
다음 코드 예제에서는 PostAndAsyncReply를 사용하는 사서함 프로세서 에이전트를 보여 줍니다.PostAndAsyncReply의 반환 값은 비동기 워크플로입니다. 이 예제에서는 회신을 처리하는 코드를 설정하기 위해 Async.StartWithContinuations를 사용하여 시작됩니다.
open System
type Message = string * AsyncReplyChannel<string>
let formatString = "Message number {0} was received. Message contents: {1}"
let agent = MailboxProcessor<Message>.Start(fun inbox ->
let rec loop n =
async {
let! (message, replyChannel) = inbox.Receive();
// Delay so that the responses come in a different order.
do! Async.Sleep( 5000 - 1000 * n);
replyChannel.Reply(String.Format(formatString, n, message))
do! loop (n + 1)
}
loop (0))
printfn "Mailbox Processor Test"
printfn "Type some text and press Enter to submit a message."
let isCompleted = false
while (not isCompleted) do
printf "> "
let input = Console.ReadLine()
let messageAsync = agent.PostAndAsyncReply(fun replyChannel -> input, replyChannel)
// Set up a continuation function (the first argument below) that prints the reply.
// The second argument is the exception continuation (not used).
// The third argument is the cancellation continuation (not used).
Async.StartWithContinuations(messageAsync,
(fun reply -> printfn "%s" reply),
(fun _ -> ()),
(fun _ -> ()))
printfn "Press Enter to continue."
Console.ReadLine() |> ignore
세션의 예를 들면 다음과 같습니다.출력이 인터리브될 수 있는데, 이는 메시지 처리 함수가 여러 스레드에서 실행되고 있다는 것을 나타냅니다.
플랫폼
Windows Windows 서버 2012, Windows Server 2008 R2, Windows 7, 8
버전 정보
F# 코어 라이브러리 버전
지원: 2.0, 4.0, 노트북