【发布时间】:2018-01-07 03:20:34
【问题描述】:
我正在尝试从 Python 程序生成 Rust 进程并将 Python 的标准输出重定向到其标准输入。我使用了以下功能:
process = subprocess.Popen(["./target/debug/mypro"], stdin=subprocess.PIPE)
并尝试使用以下方式写入子进程:
process.stdin.write(str.encode(json.dumps(dictionnaire[str(index)]))) #Write bytes of Json representation of previous track
我没有收到任何错误,但 Rust 中的标准输入似乎没有接受任何输入,标准输出根本没有打印任何内容。
这是我当前运行的 Rust 代码版本:
extern crate rustc_serialize;
use rustc_serialize::json::Json;
use std::fs::File;
use std::io;
use std::env;
use std::str;
fn main(){
let mut buffer = String::new();
let stdin = io::stdin();
//stdin.lock();
stdin.read_line(&mut buffer).unwrap();
println!{"{}", buffer};
println!{"ok"};
}
【问题讨论】:
-
听起来不错。我已将您的问题移至帖子的标题,这是它在 Stack Overflow 上的所属位置。接下来,我鼓励您在edit 您的问题中包含重要信息:程序做什么 以及您期望 它做什么。
-
“这根本不起作用” - 然后发布错误或您遇到的任何问题?不要让其他人复制你已经做过的尝试。
-
这不是使用
fork的正确方法。你应该fork然后exec替换子进程。相反,您正在调用subprocess.run,它会执行自己的fork+exec并启动外部进程。您的文件描述符在该过程中将无效。
标签: python rust ipc file-descriptor pipelining