Skip to content

Commit

Permalink
read stdout in the background
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Feb 7, 2025
1 parent b3f297a commit e4459ac
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions tokio/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,6 @@ impl Child {
/// # use tokio::io::AsyncReadExt;
/// # use tokio::process::Command;
/// # use tokio::sync::oneshot::channel;
/// #
/// # use futures::future::join;
///
/// #[tokio::main]
/// async fn main() {
Expand All @@ -1216,14 +1214,21 @@ impl Child {
/// .unwrap();
///
/// let mut stdout = child.stdout.take().expect("stdout is not captured");
/// let mut buff = Vec::new();
/// let wait_for_output = join(child.wait(), stdout.read_to_end(&mut buff));
///
/// let read_stdout = tokio::spawn(async move {
/// let mut buff = Vec::new();
/// let _ = stdout.read_to_end(&mut buff).await;
///
/// buff
/// });
///
/// tokio::select! {
/// _ = wait_for_output => {}
/// _ = rx => child.kill().await.expect("kill failed"),
/// _ = child.wait() => {}
/// _ = rx => { child.kill().await.expect("kill failed") },
/// }
///
/// let buff = read_stdout.await.unwrap();
///
/// assert_eq!(buff, b"Hello World!\n");
/// }
/// ```
Expand Down

0 comments on commit e4459ac

Please sign in to comment.