Skip to content

Instantly share code, notes, and snippets.

@zakki
Created September 14, 2012 13:22
Show Gist options
  • Select an option

  • Save zakki/3721866 to your computer and use it in GitHub Desktop.

Select an option

Save zakki/3721866 to your computer and use it in GitHub Desktop.
open Batteries_uni
let (>>=) = Lwt.bind
let http_get url =
Printf.printf "sleep\n";
Lwt_unix.sleep 2.0 >>= (fun () ->
Printf.printf "wake up\n";
Lwt.return ("data '" ^ url ^ ""));;
let process a b =
Printf.printf "a:%s\nb:%s\n" a b;;
let join2 ta tb f =
let a = ref None in
let b = ref None in
let ts = [
ta >>= (fun v -> a := Some v; Lwt.return());
tb >>= (fun v -> b := Some v; Lwt.return())
] in
Lwt.join ts >>= fun _ ->
f (Option.get !a) (Option.get !b);;
let _ =
print_endline "a";
let t =
http_get "http://hoge.com/a" >>= fun a ->
http_get "http://hoge.com/b" >>= fun b ->
process a b;
Lwt.return() in
Lwt_main.run t
let _ =
print_endline "b";
let ta = http_get "http://hoge.com/a" in
let tb = http_get "http://hoge.com/b" in
let t = join2 ta tb (fun a b ->
process a b;
Lwt.return ()) in
Lwt_main.run t
let _ =
print_endline "c";
let ta = http_get "http://hoge.com/a" in
let tb = http_get "http://hoge.com/b" in
let t = ta >>= fun a ->
tb >>= fun b ->
process a b;
Lwt.return() in
Lwt_main.run t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment