How would you stream output from a Process in Rust?

爷,独闯天下 提交于 2019-12-24 14:17:51

问题


This question refers to Rust as of October 2014.

If you are using Rust 1.0 or above, you best look elsewhere for a solution.


I have a long running Rust process that generates log values, which I'm running using Process.

It looks at though I might be able to periodically "check on" the running process using set_timeout() and wait() and do something kind of high level loop like:

let mut child = match Command::new("thing").arg("...").spawn() {
    Ok(child) => child,
    Err(e) => fail!("failed to execute child: {}", e),
};
loop {
    child.set_timeout(Some(100));
    match child.wait() {
        // ??? Something goes here
    }
}

The things I'm not 100% on are; how do I tell the difference between a timeout error and a process-return error from wait(), and how to a use the PipeStream to "read as much as you can without blocking from the stream" every interval to push out.

Is this the best approach? Should I start a task to monitor stdout and stderr instead?


回答1:


For distinguishing the errors from the process from the timeout, you have to manage the returns from wait, an example here:

fn run() {
    let mut child = match Command::new("sleep").arg("1").spawn() {
        Ok(child) => child,
        Err(e) => fail!("failed to execute child: {}", e),
    };
    loop {
        child.set_timeout(Some(1000));
        match child.wait() {
            // Here assume any error is timeout, you can filter from IoErrorKind
            Err(..) => println!("Timeout"),
            Ok(ExitStatus(0)) => {
                println!("Finished without errors");
                return;
            }
            Ok(ExitStatus(a)) => {
                println!("Finished with error number: {}", a);
                return;
            }
            Ok(ExitSignal(a)) => {
                println!("Terminated by signal number: {}", a);
                return;
            }
        }
    }
}

About using streams, check with wait_with_output, or implement something similar with channels and threads : http://doc.rust-lang.org/src/std/home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/libstd/io/process.rs.html#601

Hope it helped



来源:https://stackoverflow.com/questions/26550962/how-would-you-stream-output-from-a-process-in-rust

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!