【发布时间】:2021-01-07 17:10:28
【问题描述】:
我一直在尝试编写一个在单独的线程中调用系统命令的应用程序。
关键特征是我希望能够终止此命令并根据请求从主线程调用一个新命令。
我的辅助线程如下所示:
let (tx, rx): (mpsc::Sender<String>, mpsc::Receiver<String>) = mpsc::channel();
let child_handle = stoppable_thread::spawn(move |stop| {
let mut child = Command::new("[the program]").arg(format!("-g {}", gain)).stdout(Stdio::piped()).spawn().expect("naw man");
let mut childout = child.stdout.as_mut().unwrap();
while !stop.get() {
let mut buffer = [0; 128];
childout.try_read(&mut buffer).unwrap(); // This part makes the code wait for the next output
// Here the buffer is sent via mpsc irrelevant to the issue
}});
问题是当我发送停止信号时(或者如果我使用 mpsc 通道通知线程停止)它会等待命令将某些内容输出到标准输出。这是不受欢迎的行为。
我该如何解决这个问题?如何中断 read() 函数?
【问题讨论】:
-
没有,这个用在sn-p代码中,我的问题是关于中断read()命令。
标签: rust