Unix
这是一个使用模块与子进程交互的简单程序。我只是启动一个cat
shell 命令,向它发送一个字符串并读回它:
#load "unix.cma";; (* Needed if you are in the toplevel *)
let () =
let sin, sout, serr = Unix.open_process_full "cat" [||] in
output_string sout "test\n";
flush sout;
input_line sin |> print_string;
flush stdout;
Unix.close_process_full (sin, sout, serr) |> ignore;;
最近我开始研究这个Lwt
库,我想用它重现相同的功能。我认为以下内容应该具有完全相同的结果:
#use "topfind";; (* *)
#thread;; (* Also only for the toplevel *)
#require "lwt.simple-top";; (* *)
let () =
let open Lwt in
let process = Lwt_process.open_process_full ( "cat" , [||] ) in
Lwt_io.write_line process#stdin "test\n"
>>= ( fun () -> Lwt_io.flush process#stdin )
>>= ( fun () -> Lwt_io.read process#stdout )
>>= ( fun str -> Lwt_io.print str )
>>= ( fun () -> Lwt_io.flush Lwt_io.stdout )
|> Lwt_main.run
但它并没有像我期望的那样工作——显然它读取然后打印一个空字符串。
我想我对Lwt
应该如何工作有一些基本的困惑,但我无法弄清楚。有人可以告诉我如何使用 与子进程进行通信Lwt
吗?