Created
August 6, 2014 08:25
-
-
Save Gab-km/f4ede36324b6f6cb3fef to your computer and use it in GitHub Desktop.
MailboxProcessor<'Message> の調べ物
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Message = string * AsyncReplyChannel<string> | |
let agent = new MailboxProcessor<Message>(fun inbox -> | |
let rec loop count = | |
async { | |
System.Threading.Thread.Sleep(1000) // something busy | |
let! msg, replyChannel = inbox.Receive() | |
printfn "%s" msg | |
replyChannel.Reply(msg + msg) | |
return! loop (count + 1) | |
} | |
loop 0) | |
printfn "agent.Start()" | |
agent.Start() | |
printfn "agent.Post('Hello!')" | |
let rep1 = agent.PostAndAsyncReply(fun replyChannel -> "Hello!", replyChannel) | |
printfn "agent.Post('F#!F#!')" | |
let rep2 = agent.PostAndAsyncReply(fun replyChannel -> "F#!F#!", replyChannel) | |
[rep1 ; rep2] | |
|> Async.Parallel | |
|> Async.RunSynchronously | |
|> Array.iter (printfn "%s") | |
(** output is like following: | |
agent.Start() | |
agent.Post('Hello!') | |
agent.Post('F#!F#!') | |
Hello! | |
F#!F#! | |
Hello!Hello! | |
F#!F#!F#!F#! | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment