長時間実行している子プロセスがあり、多くのデータを読み書きする必要があります。作家が完了すると、長時間実行しているstd :: process :: Childへの読み書き
extern crate scoped_threadpool;
fn main() {
// run the subprocess
let mut child = std::process::Command::new("cat")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.unwrap();
let child_stdout = child.stdout.as_mut().unwrap();
let child_stdin = std::sync::Mutex::new(child.stdin.as_mut().unwrap());
let mut pool = scoped_threadpool::Pool::new(2);
pool.scoped(|scope| {
// read all output from the subprocess
scope.execute(move || {
use std::io::BufRead;
let reader = std::io::BufReader::new(child_stdout);
for line in reader.lines() {
println!("{}", line.unwrap());
}
});
// write to the subprocess
scope.execute(move || {
for a in 0..1000 {
use std::io::Write;
writeln!(&mut child_stdin.lock().unwrap(), "{}", a).unwrap();
} // close child_stdin???
});
});
}
読者はEOFを見ているように私は、child_stdin
ようにサブプロセスが終了し、終了を閉じたいと:私は、リーダー・スレッドとそれぞれchild.stdout
とchild.stdin
を操作ライター・スレッドを持っていますpool.scoped
が返されます。私はchild.wait()
なしでこれを行うことはできません。child.wait()
は2つのスレッドによって借用されているため、私はchild.wait()
に電話することはできません。
このプログラムを完成させるにはどうすればよいですか?
あなたは子プロセスに固定された文字列を作成している場合は、[サブプロセス](https://crates.io/crates/subprocess)を検討する必要がありますこれは、あなたがスレッドでやっていることに対して、['' '' ''(https://docs.rs/subprocess/0.1.11/subprocess/struct.Popen.html#method.communicate)メソッドを実装しています。また、上記を 'let input =(0..1000).map(| i | format!(" {} "、i))と表現するビルダースタイルのAPIも公開しています。免責条項:私はサブプロセスの作者です。私はサブプロセスの作成者です。 – user4815162342