Hi all,
I'm trying to use Async_parallel to distribute my computation across multiple threads/cores, following the tutorial found here:
https://blogs.janestreet.com/async-parallel. However, I don't think I'm doing it right, because even though two child processes are created, they don't appear to be running in parallel (e.g., I would expect the "Got Ping" messages to be interleaved and use different client IDs). Please see the below code (or
https://gist.github.com/amaloz/7cd0bbde736b0a06db4d). Any ideas of what I'm doing wrong? Thanks for the help!
Alex
P.S. Is this the best place to post ocaml-core questions?
Code:
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 ())