mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Use threads to avoid blocking reads/writes in externals. (#1440)
In particular, one thing that we can't (properly) do before this commit is consuming an infinite input stream. For example: ``` yes | grep y | head -n10 ``` will give 10 "y"s in most shells, but blocks indefinitely in nu. This PR resolves that by doing blocking I/O in threads, and reducing the `await` calls we currently have in our pipeline code.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
use std::io::{self, BufRead};
|
||||
use std::io::{self, BufRead, Write};
|
||||
|
||||
fn main() {
|
||||
if did_chop_arguments() {
|
||||
@ -8,9 +8,12 @@ fn main() {
|
||||
|
||||
// if no arguments given, chop from standard input and exit.
|
||||
let stdin = io::stdin();
|
||||
let mut stdout = io::stdout();
|
||||
for line in stdin.lock().lines() {
|
||||
if let Ok(given) = line {
|
||||
println!("{}", chop(&given));
|
||||
if let Err(_e) = writeln!(stdout, "{}", chop(&given)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
13
crates/nu-test-support/src/bins/iecho.rs
Normal file
13
crates/nu-test-support/src/bins/iecho.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use std::io::{self, Write};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
|
||||
// println! panics if stdout gets closed, whereas writeln gives us an error
|
||||
let mut stdout = io::stdout();
|
||||
let _ = args
|
||||
.iter()
|
||||
.skip(1)
|
||||
.cycle()
|
||||
.try_for_each(|v| writeln!(stdout, "{}", v));
|
||||
}
|
Reference in New Issue
Block a user