Last active
August 29, 2015 14:21
-
-
Save lenadroid/d76fe1205f9400026063 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // initial creation of channel and ports | |
| let channel = cluster.StoreClient.Channel | |
| let sendPort1, receivePort1 = channel.Create<string>() | |
| let sendPort2, receivePort2 = channel.Create<string>() | |
| // cloud job gets incoming messaages from the receive port, does something | |
| // with received messages and sends the results to the send port | |
| let replicateMessage (receive : IReceivePort<string>) (send : ISendPort<string>) = | |
| cloud { | |
| while true do | |
| let! result = Cloud.Catch <| receive.Receive() | |
| let randomNumber = (new System.Random()).Next(1, 7) | |
| match result with | |
| | Choice1Of2 x -> do! send.Send(String.replicate randomNumber x) | |
| | Choice2Of2 _ -> () | |
| } | |
| let job = cluster.CreateProcess(replicateMessage receivePort1 sendPort2) | |
| // here we send messages | |
| let sendSomething message = async { return! channel.SendAsync(sendPort1, message) } | |
| // here we receive messages | |
| let receiveMessages = async { | |
| while true do | |
| let! result = channel.ReceiveAsync(receivePort2) | |
| printfn "Received: %A" result | |
| } | |
| // start receiving response messages | |
| let receiveCancel = new System.Threading.CancellationTokenSource() | |
| Async.Start(receiveMessages, cancellationToken = receiveCancel.Token) | |
| // send some message | |
| sendSomething "FSharp "|> Async.Start | |
| sendSomething "Cloud " |> Async.Start | |
| sendSomething "Monad " |> Async.Start | |
| // kill all jobs | |
| receiveCancel.Cancel() | |
| job.Kill() | |
| channel.Delete(receivePort1) | |
| channel.Delete(receivePort2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment