Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: add example for reading Child stdout #7141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

maminrayej
Copy link
Member

Resolves #7138.

@maminrayej maminrayej added T-docs Topic: documentation A-tokio Area: The main tokio crate M-process Module: tokio/process labels Feb 6, 2025
Comment on lines +1218 to +1228
/// 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));
///
/// tokio::select! {
/// _ = wait_for_output => {}
/// _ = rx => child.kill().await.expect("kill failed"),
/// }
///
/// assert_eq!(buff, b"Hello World!\n");
/// }
Copy link
Contributor

@Darksonn Darksonn Feb 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what I would recommend is that you read the output from a background task. Something along these lines:

let stdout_task = tokio::spawn(async { /* read stdout here */ });
tokio::select! {
     _ = child.wait() => {},
     _ = kill_signal => child.kill().await?,
};
let output = stdout_task.await?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-process Module: tokio/process T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

process: document how to read output from Child
2 participants