How would you stream output from a Process?

后端 未结 2 1130
天涯浪人
天涯浪人 2021-01-02 01:41

I believe I understand, in general, one way of doing this:

  • Create a Command
  • Use Stdio::piped() to create a new pair of outpu
相关标签:
2条回答
  • 2021-01-02 01:54

    I'll happily accept any example of spawning a long running process and streaming output to the console, by whatever means.

    It sounds like you want Stdio::inherit:

    use std::process::{Command, Stdio};
    
    fn main() {
        let mut cmd =
            Command::new("cat")
            .args(&["/usr/share/dict/web2"])
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()
            .unwrap();
    
        // It's streaming here
    
        let status = cmd.wait();
        println!("Exited with status {:?}", status);
    }
    
    0 讨论(0)
  • 2021-01-02 02:05

    Although the accepted answer is correct, it doesn't cover the non-trivial case.

    To stream output and handle it manually, use Stdio::piped() and manually handle the .stdout property on the child returned from calling spawn, like this:

    use std::process::{Command, Stdio};
    use std::path::Path;
    use std::io::{BufReader, BufRead};
    
    pub fn exec_stream<P: AsRef<Path>>(binary: P, args: Vec<&'static str>) {
        let mut cmd = Command::new(binary.as_ref())
            .args(&args)
            .stdout(Stdio::piped())
            .spawn()
            .unwrap();
    
        {
            let stdout = cmd.stdout.as_mut().unwrap();
            let stdout_reader = BufReader::new(stdout);
            let stdout_lines = stdout_reader.lines();
    
            for line in stdout_lines {
                println!("Read: {:?}", line);
            }
        }
    
        cmd.wait().unwrap();
    }
    
    #[test]
    fn test_long_running_process() {
        exec_stream("findstr", vec!("/s", "sql", "C:\\tmp\\*"));
    }
    

    See also Merge child process stdout and stderr regarding catching the output from stderr and stdout simultaneously.

    0 讨论(0)
提交回复
热议问题