我正在尝试在 Ocaml 中为基于终端的应用程序构建一个新的前端。主要思想是使用 Lwt 生成一个新进程:
let cmd = shell "./otherterminalapp" in
let p = open_process_full cmd;
然后稍后将内容写入进程的标准输入,以在外部应用程序中执行命令。
Lwt_io.write_line p#stdin "some command" >>= (fun _ -> Lwt_io.flush p#stdin)
当我从命令中读取结果时Lwt_io.read_line_opt
. 我如何阅读直到没有任何行?我遇到的问题是我的程序只是在某个点挂起。当我阅读时read_line_opt
,当我到达终点时,它似乎只是在等待重定向新输出的过程。
我该如何处理?
我正在尝试做的一个具体示例:(基于终端的应用程序是 ocamldebug)
程序源代码:
open Lwt
open Lwt_unix
open Lwt_process
let () =
let run () =
let cmd = shell "ocamldebug test.d.byte" in
let dbgr = open_process_full cmd in
(((((((Lwt_io.write_line dbgr#stdin "info modules") >>=
(fun _ -> Lwt_io.flush dbgr#stdin))
>>= (fun _ -> Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt.return ()) in
Lwt_main.run (run ())
如果您通常使用 运行ocamldebug
,test.d.byte
您会在终端中获得以下信息:
OCaml Debugger version 4.03.0
(ocd) info modules
Loading program... done.
Used modules:
Std_exit Test Pervasives CamlinternalFormatBasics
(ocd)
当我执行上述程序时,我会打印以下内容:
OCaml Debugger version 4.03.0
(ocd) Loading program... Used modules:
Std_exit Test Pervasives CamlinternalFormatBasics
在这里它只是挂起......,我的程序没有退出。即使我在终端中执行 Ctrl-c/Ctrl-c,也会有一个活动的 ocamlrun 进程。然而,终端变得响应。
我在这里遗漏了一些明显的东西?