Skip to content

Instantly share code, notes, and snippets.

@Gab-km
Created August 6, 2014 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gab-km/f4ede36324b6f6cb3fef to your computer and use it in GitHub Desktop.
Save Gab-km/f4ede36324b6f6cb3fef to your computer and use it in GitHub Desktop.
MailboxProcessor<'Message> の調べ物
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