process: document remote killing for Child (#2736)

* process: document remote killing for Child

Fixes: #2703
This commit is contained in:
Blas Rodriguez Irizar
2020-08-05 00:59:10 +00:00
committed by GitHub
parent 7276d47072
commit 1167c09ae8
+26
View File
@@ -766,6 +766,32 @@ impl Child {
/// Forces the child to exit.
///
/// This is equivalent to sending a SIGKILL on unix platforms.
///
/// If the child has to be killed remotely, it is possible to do it using
/// a combination of the select! macro and a oneshot channel. In the following
/// example, the child will run until completion unless a message is sent on
/// the oneshot channel. If that happens, the child is killed immediately
/// using the `.kill()` method.
///
/// ```no_run
/// use tokio::process::Command;
/// use tokio::sync::oneshot::channel;
///
/// #[tokio::main]
/// async fn main() {
/// let (send, recv) = channel::<()>();
/// let mut child = Command::new("sleep").arg("1").spawn().unwrap();
/// tokio::spawn(async move { send.send(()) });
/// tokio::select! {
/// _ = &mut child => {}
/// _ = recv => {
/// &mut child.kill();
/// // NB: await the child here to avoid a zombie process on Unix platforms
/// child.await.unwrap();
/// }
/// }
/// }
pub fn kill(&mut self) -> io::Result<()> {
self.child.kill()
}