rust - 如何在 Rust 中杀死 Ctrl+C 事件的进程?
问题描述
我正在尝试Ctrl+C使用ctrlc包覆盖该事件。我想在事件中杀死一个子进程。我目前正在这样做。
let mut child = Command::new(shell)
.arg(option)
.arg(script)
.spawn()
.unwrap();
ctrlc::set_handler(move || {
println!("Received Ctrl+C!");
child.kill().expect("Couldn't kill the process!");
})
.expect("Error setting interrupt handler!");
child.wait().unwrap();
但我有错误cannot borrow child as mutable, as it is a captured variable in a Fn closure
。错误的含义是什么,我该如何解决?
解决方案
set_handler
启动“一个新的专用信号处理线程”。
似乎set_handler
expects Fn
,与此相反FnMut
- 不能被突变。因为它不能变异,所以它不能变异捕获的child
.
您可以尝试child
在闭包内定义并与外线程通信。
您可以尝试在线程之间建立通信(例如使用mpsc::channel
),而不是在主线程中使用wait
for 。child
您可以尝试检索stdout / stderr而不是wait
ing。
推荐阅读
- python - 使用 OpenCV 将 RGB 图像转换为灰度图像时,为什么会得到绿色图像?
- sql - 将表从 Oracle 迁移到 SQL Server
- vba - 与 sumif 类似的连接代码
- python - 为什么我的 Python 和 pip 在不同的地方?
- java - SimpleJdbcCall Oracle 存储过程错误的参数数量或类型
- python - csv 列/未知数据中的连字符导致 int 转换错误
- typescript - 如何等待多个可观察流解决并一起返回它们
- python - 如何在运行时撤销任务
- c# - 当我下载太快时,C# Webclient().OpenRead 是连接返回 404 错误
- c++ - 读取 HDFql 中的属性值