Skip to content

Instantly share code, notes, and snippets.

@amaloz
Created January 12, 2015 15:17
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 amaloz/7cd0bbde736b0a06db4d to your computer and use it in GitHub Desktop.
Save amaloz/7cd0bbde736b0a06db4d to your computer and use it in GitHub Desktop.
Attempt at parallelizing OCaml computation
open Core.Std
open Async.Std
open Async_parallel.Std
let workers = ["localhost"; "localhost"]
let worker h =
Pipe.iter_without_pushback (Hub.listen_simple h) ~f:(fun (id, `Ping) ->
printf "%s Got Ping\n%!" (Hub.Client_id.sexp_of_t id |> Sexp.to_string_hum);
Hub.send h id `Pong)
>>| fun () -> `Done
let main () =
printf "Start\n%!";
Deferred.List.iter [1; 2; 3; 4] ~f:(fun i ->
Parallel.spawn ~where:Parallel.round_robin worker
>>= fun (c, res) ->
printf "Value = %d\n%!" i;
let rec loop = function
| 0 -> Channel.close c
| i ->
Channel.write c `Ping;
Channel.read c
>>= fun `Pong ->
Clock.after (sec 1.)
>>= fun _ -> loop (i - 1)
in
loop 2
)
>>> fun _ -> Shutdown.shutdown 0
let _ =
Parallel.init ~cluster:{ Cluster.master_machine = Unix.gethostname ();
worker_machines = workers } ();
main ();
never_returns (Scheduler.go ())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment