No buffering cmd output when running on 1 thread
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Max Triano · committed by David Peter · parent 0f0fa76 · Browse files · 4 files·+30 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+30 added−31 removed
- other:+30 added−31 removed
Provenance
- Commit
394e967- Tree
6ad43a1- Author
Max Triano <mtriano234@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
0f0fa76
4 files changed+30 added−31 removed
modified:src/exec/command.rs+14 added-21 removed
5 unmodified lines6 use crate::error::print_error;7 use crate::exit_codes::ExitCode;8 9 /// Executes a command.10-pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>) -> ExitCode {10+pub fn execute_command(mut cmd: Command, out_perm: &Mutex<()>, is_multithread: bool) -> ExitCode {11+ let output;12 // Spawn the supplied command.12- /* let output = cmd.output(); */13- // This sort of works:14- let child = cmd.spawn().expect("failed to wait on child");15- let output = child16- .wait_with_output()17- .expect("failed to wait on child"); 18- ExitCode::Success19- /* let child_handle = cmd.spawn();20- match child_handle {21- Ok(output) => {22- let exit_code = output.wait();23- }24- Err() => {25-26- }27- } */13+ if is_multithread {14+ output = cmd.output();15+ } else {16+ // This sort of works:17+ let child = cmd.spawn().expect("Failed to execute command");18+ output = child.wait_with_output();19+ }20+ 21 // Then wait for the command to exit, if it was spawned.29- /* match output {22+ match output {23 Ok(output) => {24 // While this lock is active, this thread will be the only thread allowed25 // to write its outputs.26 let _lock = out_perm.lock().unwrap();27 28 let stdout = io::stdout();29 let stderr = io::stderr();30 38- //let _ = stdout.lock().write_all(&output.stdout);39- //let _ = stderr.lock().write_all(&output.stderr);31+ let _ = stdout.lock().write_all(&output.stdout);32+ let _ = stderr.lock().write_all(&output.stderr);33 34 if output.status.code() == Some(0) {35 ExitCode::Success36 } else {7 unmodified lines44 Err(why) => {45 print_error(format!("Problem while executing command: {}", why));46 ExitCode::GeneralError47 }55- } */48+ }49 }modified:src/exec/job.rs+5 added-3 removed
14 unmodified lines15 rx: Arc<Mutex<Receiver<WorkerResult>>>,16 cmd: Arc<CommandTemplate>,17 out_perm: Arc<Mutex<()>>,18 show_filesystem_errors: bool,19+ is_multithread: bool,20 ) -> ExitCode {21 let mut results: Vec<ExitCode> = Vec::new();22 loop {23 // Create a lock on the shared receiver for this thread.10 unmodified lines34 continue;35 }36 Err(_) => break,37 };37-38+ 39 // Drop the lock so that other threads can read from the receiver.40 drop(lock);41 // Generate a command, execute it and store its exit code.41- results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm)))42+ results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm), is_multithread))43 }44 // Returns error in case of any error.45 merge_exitcodes(&results)46 }1 unmodified line48 pub fn batch(49 rx: Receiver<WorkerResult>,50 cmd: &CommandTemplate,51 show_filesystem_errors: bool,52+ is_multithread: bool,53 ) -> ExitCode {54 let paths = rx.iter().filter_map(|value| match value {55 WorkerResult::Entry(val) => Some(val),56 WorkerResult::Error(err) => {2 unmodified lines59 }60 None61 }62 });61- cmd.generate_and_execute_batch(paths)63+ cmd.generate_and_execute_batch(paths, is_multithread)64 }modified:src/exec/mod.rs+4 added-4 removed
137 unmodified lines138 /// Generates and executes a command.139 ///140 /// Using the internal `args` field, and a supplied `input` variable, a `Command` will be141 /// build. Once all arguments have been processed, the command is executed.142- pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>) -> ExitCode {142+ pub fn generate_and_execute(&self, input: &Path, out_perm: Arc<Mutex<()>>, is_multithread: bool) -> ExitCode {143 let input = strip_current_dir(input);144 145 let mut cmd = Command::new(self.args[0].generate(&input, self.path_separator.as_deref()));146 for arg in &self.args[1..] {147 cmd.arg(arg.generate(&input, self.path_separator.as_deref()));148 }149 150- execute_command(cmd, &out_perm)150+ execute_command(cmd, &out_perm, is_multithread)151 }152 153 pub fn in_batch_mode(&self) -> bool {154 self.mode == ExecutionMode::Batch155 }156 157- pub fn generate_and_execute_batch<I>(&self, paths: I) -> ExitCode157+ pub fn generate_and_execute_batch<I>(&self, paths: I, is_multithread: bool) -> ExitCode158 where159 I: Iterator<Item = PathBuf>,160 {161 let mut cmd = Command::new(self.args[0].generate("", None));19 unmodified lines181 }182 }183 184 if has_path {185- execute_command(cmd, &Mutex::new(()))185+ execute_command(cmd, &Mutex::new(()), is_multithread)186 } else {187 ExitCode::Success188 }189 }268 unmodified linesmodified:src/walk.rs+7 added-3 removed
168 unmodified lines169 let wants_to_quit = Arc::clone(wants_to_quit);170 171 let show_filesystem_errors = config.show_filesystem_errors;172 let threads = config.threads;173-173+ let is_multithread: bool = if threads > 1 {174+ true175+ } else {176+ false177+ };178 thread::spawn(move || {179 // This will be set to `Some` if the `--exec` argument was supplied.180 if let Some(ref cmd) = config.command {181 if cmd.in_batch_mode() {178- exec::batch(rx, cmd, show_filesystem_errors)182+ exec::batch(rx, cmd, show_filesystem_errors, is_multithread)183 } else {184 let shared_rx = Arc::new(Mutex::new(rx));185 186 let out_perm = Arc::new(Mutex::new(()));6 unmodified lines193 let out_perm = Arc::clone(&out_perm);194 195 // Spawn a job thread that will listen for and execute inputs.196 let handle =193- thread::spawn(move || exec::job(rx, cmd, out_perm, show_filesystem_errors));197+ thread::spawn(move || exec::job(rx, cmd, out_perm, show_filesystem_errors, is_multithread));198 199 // Push the handle of the spawned thread into the vector for later joining.200 handles.push(handle);201 }290 unmodified lines