Skip to content

Instantly share code, notes, and snippets.

@alinpopa
Created April 30, 2017 10:09
Show Gist options
  • Save alinpopa/bd6ff90ae1e787e2c4044de84c79e2b4 to your computer and use it in GitHub Desktop.
Save alinpopa/bd6ff90ae1e787e2c4044de84c79e2b4 to your computer and use it in GitHub Desktop.
(*
*Compile with:
*ocamlfind ocamlc -o test_lwt -thread -linkpkg -package core,lwt,lwt.unix test_lwt.ml
*Run with:
*./test_lwt
*)
open Lwt.Infix
open Core.Std
let rec read_data ic =
Lwt_io.read_line ic >>= fun v ->
print_endline ("Data: " ^ v);
read_data ic
let read_numbers ic =
let rec loop () = Lwt_io.read_int ic >>= fun v ->
print_endline ("Int: " ^ (string_of_int v));
loop ()
in
Lwt.async (loop)
let ping oc =
let rec loop () =
Lwt_unix.sleep 1.0 >>= fun () ->
ignore (Lwt_io.write_line oc "Some message");
let _ = Lwt_io.write_line oc "Ping" in
loop ()
in
Lwt.async (loop)
let pings oc =
let rec loop () =
Lwt_unix.sleep 1.0 >>= fun () ->
ignore (Lwt_io.write_int oc 7);
loop ()
in
Lwt.async (loop)
let other_stuff oc =
let rec loop () =
Lwt_unix.sleep 3.0 >>= fun () ->
let _ = Lwt_io.write_line oc "Some message..." in
loop ()
in
Lwt.async (loop)
let () =
let (ic, oc) = Lwt_io.pipe () in
let (ic2, oc2) = Lwt_io.pipe () in
ignore (Lwt_io.write_line oc "msg1");
ignore (Lwt_io.write_line oc "msg2");
ignore (Lwt_io.write_line oc "5");
ping oc;
other_stuff oc;
pings oc2;
read_numbers ic2;
Lwt_main.run (read_data ic)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment