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:
Jason Gedge
2020-03-01 12:19:09 -05:00
committed by GitHub
parent ca615d9389
commit 7304d06c0b
10 changed files with 273 additions and 71 deletions

View File

@ -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;
}
}
}

View 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));
}