Last active
June 8, 2024 15:33
-
-
Save cowlike/9817169e54227f33c28a9b89da21178c to your computer and use it in GitHub Desktop.
simple example using an F# MailboxProcessor
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 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