I have the following Rust code that sends a string to a subprocess via stdin
and read whatever comes from it's stdout
:
use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio};
fn main() {
let mut child = Command::new("cat")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Did not run");
let v = vec!["11111", "2222222", "3333", "end", "soooooooooooooooo"];
let k = child.stdin.as_mut().unwrap();
let mut g = BufReader::new(child.stdout.as_mut().unwrap());
for x in v.into_iter() {
k.write_all(x.as_bytes()).unwrap();
k.write_all("\n".as_bytes()).unwrap();
let mut s: String = String::new();
g.read_line(&mut s).unwrap();
println!("{}", s)
}
}
The example runs without issues but when I try to do the same using a python script it fails.
To call the script I'm creating child
like this:
let mut child = Command::new("c:\\Windows\\py.exe")
.args(&["-u", "echo.py"])
And in the script I'm just echoing whatever comes from stdin
to stdout
import sys
import time
if __name__=="__main__":
fo=sys.stdout
fo.write('python starts at: %.0f\n'%(time.time()*1000))
line = sys.stdin.readline()
while True:
fo.write(line+"\n")
line = sys.stdin.readline()
fo.write('python ends at: %.0f\n'%(time.time()*1000))
The output I'm getting is:
python starts at: 1582166542281
11111
2222222
Traceback (most recent call last):
File "c:\Users\asia\Documents\projects\python\echo.py", line 8, in <module>
fo.write(line+"\n")
OSError: [Errno 22] Invalid argument
My take is the pipe breaks somewhere around printing the second string but I can't figure out why. What am I missing here?
Thanks in advance