-
If I open a child process with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The general approach for merging the two streams is the following:
This gives the following code: use tokio::io::{AsyncBufReadExt, BufReader};
use tokio_stream::StreamExt;
use tokio_stream::wrappers::LinesStream;
// Variable of type tokio::process::Child.
let child = ...;
// Take ownership of stdout and stderr from child.
let stdout = child.stdout.take().unwrap();
let stderr = child.stderr.take().unwrap();
// Wrap them up and merge them.
let stdout = LinesStream::new(BufReader::new(stdout).lines());
let stderr = LinesStream::new(BufReader::new(stderr).lines());
let mut merged = StreamExt::merge(stdout, stderr);
// Iterate through the stream line-by-line.
while let Some(line) = merged.next().await {
// Since reading a line may fail, we use the question-mark to unwrap the line.
let line = line?;
println!("{}", line);
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for looking into this, Deno has a need for this feature.
Systems which support combining pipes can do better than buffering line-by-line. Running the following script should show that shells multiplex the two streams more granularly: #!/bin/bash
printf "abc"
printf "def" 1>&2
printf "ghi"
printf "jkl" 1>&2
The following shows that Python 3's pipe combination can do the same: #!/usr/bin/env python3
import subprocess
print(subprocess.run(["./temp.sh"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT))
Ultimately, I think we need a mirror of https://docs.rs/os_pipe/0.9.2/os_pipe/ where https://docs.rs/os_pipe/0.9.2/os_pipe/struct.PipeReader.html can be asynchronously read. |
Beta Was this translation helpful? Give feedback.
The general approach for merging the two streams is the following:
tokio::io::BufReader
.tokio::io::Lines
.tokio_stream::wrappers::LinesStream
.tokio_stream::StreamExt::merge
.This gives the following code: