Skip to content

Instantly share code, notes, and snippets.

@cowlike
Last active June 8, 2024 15:33
Show Gist options
  • Save cowlike/9817169e54227f33c28a9b89da21178c to your computer and use it in GitHub Desktop.
Save cowlike/9817169e54227f33c28a9b89da21178c to your computer and use it in GitHub Desktop.
simple example using an F# MailboxProcessor
type Msg =
| Msg of string
| Done of AsyncReplyChannel<int>
let printerAgent = MailboxProcessor.Start(fun inbox->
// the message processing function
let rec messageLoop() = async{
let! msg = inbox.Receive()
match msg with
| Msg s -> printfn "message is: %s" s
| Done rc -> rc.Reply(0)
return! messageLoop()
}
// start the loop
messageLoop()
)
let xs = List.map (fun n -> sprintf "hello %d" n) [1..30]
for s in xs do
printerAgent.Post(Msg s)
printerAgent.PostAndReply(fun rc -> Done rc)
|> printfn "exit code: %d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment