Unix
モジュールを使用してサブプロセスと対話する簡単なプログラムです。私はちょうど、cat
シェルコマンドを起動し、それを文字列を送信し、それをリードバック:LWTサブプロセスとの簡単な対話
#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
を使用してサブプロセスと通信する方法を誰かに教えてもらえますか?私はあなたが適切な方法でプログラムを実行します後、あなたがなることを、疑い、また
Lwt_process.shell "cat";;
- : Lwt_process.command = ("", [|"/bin/sh"; "-c"; "cat"|])
:
あなたはそれを間違って渡しています: 'Lwt_process.open_process_full(" cat "、[" cat ";" Makefile "|))' –
@EdgarAroutiounian彼は引数なしで 'cat'を実行しています。 stdinをstdout( 'cat'の)にコピーします。 – RichN
@EdgarAroutiounian Yeah' [| "cat"; "Makefile" |] 'は動作し、Makefileをダンプします。しかし、これは私の望むものではありません。@ RichNは言っていますが、 'cat'をstdin - > stdoutプログラムとして使いたいと思います。私は '[|" cat "|]'を試みました - それはいくつかの奇抜な理由でプログラムをブロックしています... – Kostya