oconnor663 / shared_child.rs

a wrapper around std::process::Child that lets multiple threads wait or kill at once
MIT License
39 stars 7 forks source link

Async impl #21

Open 2A5F opened 1 year ago

dishmaker commented 10 months ago

You can drop a Future from another Future in tokio so this is kinda solved.

https://stackoverflow.com/questions/30538004/how-do-i-ensure-that-a-spawned-child-process-is-killed-if-my-app-panics

Just call start_kill(&mut self) from tokio Child in Drop impl


struct ChildGuard(Child);

impl Drop for ChildGuard {
    fn drop(&mut self) {
        self.0.start_kill();
    }
}
async fn my_async_process() {
    let mut child = Command::new("sleep").arg("1").spawn().unwrap();
    let mut child = SharedChild(child);
    let _ = child.wait().await; // here you can kill by dropping future
}

async fn main() {
    let fut = my_async_process();

    // drop for example by `select!` or something else
    drop(fut);
}