I suspect that @steffahn has the correct answer here.
For future reference, it’s usually easier to help with your problems if you describe what the buggy code is doing in addition to what you expect. Here, that could be something like:
When I add a loop, it stops printing anything.
This lets us focus on the problem you’re actually having instead of all the possble problems you might have. I, for example, immediately started looking for possible sources of compile errors.
It also makes it easier for others having the same problem to find this thread in the future.
fn start_listener(child: &mut Child, sender: Sender<String>) -> JoinHandle<()> {
let mut stdin = child.stdin.take().unwrap();
let stdout = child.stdout.take().unwrap();
let mut f = BufReader::new(stdout);
return thread::spawn(move || loop {
let mut buf = String::new();
match f.read_line(&mut buf) {
Ok(_) => {
stdin.write_all("2".as_bytes()).unwrap();
sender.send(buf).unwrap();
}
Err(e) => println!("an error!: {:?}", e),
}
});
}
I have a sub-process, which is running in the background continuously, I have a function which takes a mutable reference to this process and in a thread it loops it over, to check for new lines. If there is a new line, I want to send back an stdin.
In the code above, the code compiles, the childprocess works, and when stdout occures, it sends the content via the sender channel. But the stdin.write_all() does not work at all. There is no error message also.
@alice grrrr, you made my day . I changed write_all(b"2") to write_all(b"2\n") and it works. So the new line solved it.
I am kind of new to the process stdin and stdout, and it was soo confusing to not understand why does not happening anything.
I knew something wrong, is because the child process did not receive the content. But if I just tested it without the thread part, it worked.
It also strange, that if I do not use a new line, but nor the thread part, it works. If I use it inside a thread with a loop, then I need to apply the new line character. Do you understand why?
There’s two ways that a program can know that a line of input is complete: it can see a newline character or the input stream can be closed. The loop holds the stream open so that it can be used again later, which means you need to use the newline method.