Skip to content

Instantly share code, notes, and snippets.

@owskio
Created November 3, 2015 22:21
Show Gist options
  • Save owskio/b16568415e1b7995a289 to your computer and use it in GitHub Desktop.
Save owskio/b16568415e1b7995a289 to your computer and use it in GitHub Desktop.
//Modified from: http://fsharpforfunandprofit.com/posts/concurrency-actor-model
open System
let print str = printfn "%A" str
let slowWriter msg =
msg |> String.iter (fun ch->
Threading.Thread.Sleep(1)
Console.Write ch
)
Console.WriteLine()
let makeTask log taskId = async {
for i in [1..3] do
log <| sprintf " [Task%i:Loop%i] " taskId i
}
print "=============="
[1..5]
|> List.map (fun i -> makeTask slowWriter i)
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
print "=============="
let agent = MailboxProcessor.Start(fun inbox ->
async {
while true do
let! msg = inbox.Receive()
slowWriter msg
})
[1..5]
|> List.map (fun i -> makeTask agent.Post i)
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
Console.ReadKey() |> ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment