#process #child #wait #command #clone

已删除 clonablechild

扩展并包装 std::process::Child 以使其可克隆

使用旧的 Rust 2015

0.1.1 2017年1月5日
0.1.0 2016年12月23日

#40 in #child

MIT/Apache

9KB
121 代码行

ClonableChild

扩展并包装 std::process::Child 以使其可克隆。从而消除了当使用 libstd 时没有跨平台且简单的方法来杀死子进程,同时等待其结束的问题。

入门指南

将依赖项添加到您的 Cargo.toml 中

[dependencies]
clonablechild = "0.1"

示例

在您的程序中使用它来在自然终止之前杀死睡眠进程

fn main() {
    // This command is specific to unix systems. See tests for Windows examples.
    let child = Command::new("sleep").arg("10").spawn().unwrap();
    let clonable_child = child.into_clonable();

    kill_async(clonable_child.clone());
    let exit_status = clonable_child.wait().unwrap();

    // Assert child was killed by a signal and did not exit cleanly
    assert_eq!(None, exit_status.code());
    assert!(!exit_status.success());
}

fn kill_async(child: ClonableChild) {
    thread::spawn(move || {
        thread::sleep(Duration::new(1, 0));
        child.kill().expect("Expected to be able to kill subprocess");
    });
}

依赖项

~43KB