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

Add "Process child process output continuously" example #373

Merged
merged 2 commits into from Dec 1, 2017

Conversation

Projects
None yet
2 participants
@ludwigpacifici
Copy link
Contributor

ludwigpacifici commented Nov 27, 2017

fixes #339

Hello @budziq ! Another PR :-) I also added a commit to update the std badge version.

@budziq
Copy link
Collaborator

budziq left a comment

Thanks @ludwigpacifici !

Some suggestions below

println!("{}", line);
}
}
#

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

the Ok(()) might be a boilerplate but lets not hide it as it is necessary to compile the example when copy pasted

@@ -12,6 +12,7 @@
| [Run an external command passing it stdin and check for an error code][ex-parse-subprocess-input] | [![regex-badge]][regex] | [![cat-os-badge]][cat-os] [![cat-text-processing-badge]][cat-text-processing] |
| [Run piped external commands][ex-run-piped-external-commands] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
| [Redirect both stdout and stderr of child process to the same file][ex-redirect-stdout-stderr-same-file] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |
| [Continuously process child process' outputs][ex-continuous-process-output] | [![std-badge]][std] | [![cat-os-badge]][cat-os] |

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

hmm text-processing badge might make sense. What are your thoughts @ludwigpacifici ?

This comment has been minimized.

@ludwigpacifici

ludwigpacifici Nov 29, 2017

Author Contributor

Yep. Plus it will be homogeneous with similar recipes like Run an external command and process stdout.

let reader = BufReader::new(stdout);
for line in reader.lines() {

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

how about we try to express this in a functional style with filter_map filter and for_each?

# extern crate error_chain;
#
use std::process::{Command, Stdio};
use std::io::BufReader;

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

lets group these imports

This comment has been minimized.

@ludwigpacifici

ludwigpacifici Nov 29, 2017

Author Contributor

hmm, I remember rustfmt was doing this for me before. But not now. Anyway, let's group them.

stdout](#ex-parse-subprocess-output), while an external [`Command`] is
running, process its standard output. A new pipe is created by
[`Stdio::piped`] and it is read by a [`BufReader`]. Note, a buffered
reader uses a buffer to reduce the number of I/O requests.

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

I'm not sure that we need to explain what BufReader does

This comment has been minimized.

@ludwigpacifici

ludwigpacifici Nov 28, 2017

Author Contributor

True, it's slightly off topic. While I was implementing the example, I ask myself the question std::io::BufReader vs std::io::Reader. I will remove it. Plus the BufReader link to the doc explains it:

It can be excessively inefficient to work directly with a Read instance. For example, every call to read on TcpStream results in a system call. A BufReader performs large, infrequent reads on the underlying Read and maintains an in-memory buffer of the results.


Contrary to [Run an external command and process
stdout](#ex-parse-subprocess-output), while an external [`Command`] is
running, process its standard output. A new pipe is created by

This comment has been minimized.

@budziq

budziq Nov 28, 2017

Collaborator

the explanation is slightly unclear. I'd emphasize that the we do not wait for the child process to finish and process the output continuously.

@ludwigpacifici ludwigpacifici force-pushed the ludwigpacifici:master branch from 0329fa5 to 603c608 Nov 29, 2017

@budziq budziq merged commit 8543f72 into rust-lang-nursery:master Dec 1, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.