@@ -1169,6 +1169,8 @@ impl Child {
1169
1169
///
1170
1170
/// This is equivalent to sending a `SIGKILL` on unix platforms.
1171
1171
///
1172
+ /// # Examples
1173
+ ///
1172
1174
/// If the child has to be killed remotely, it is possible to do it using
1173
1175
/// a combination of the select! macro and a `oneshot` channel. In the following
1174
1176
/// example, the child will run until completion unless a message is sent on
@@ -1190,6 +1192,46 @@ impl Child {
1190
1192
/// }
1191
1193
/// }
1192
1194
/// ```
1195
+ ///
1196
+ /// You can also interact with the child's standard I/O. For example, you can
1197
+ /// read its stdout while waiting for it to exit.
1198
+ ///
1199
+ /// ```no_run
1200
+ /// # use std::process::Stdio;
1201
+ /// #
1202
+ /// # use tokio::io::AsyncReadExt;
1203
+ /// # use tokio::process::Command;
1204
+ /// # use tokio::sync::oneshot::channel;
1205
+ ///
1206
+ /// #[tokio::main]
1207
+ /// async fn main() {
1208
+ /// let (_tx, rx) = channel::<()>();
1209
+ ///
1210
+ /// let mut child = Command::new("echo")
1211
+ /// .arg("Hello World!")
1212
+ /// .stdout(Stdio::piped())
1213
+ /// .spawn()
1214
+ /// .unwrap();
1215
+ ///
1216
+ /// let mut stdout = child.stdout.take().expect("stdout is not captured");
1217
+ ///
1218
+ /// let read_stdout = tokio::spawn(async move {
1219
+ /// let mut buff = Vec::new();
1220
+ /// let _ = stdout.read_to_end(&mut buff).await;
1221
+ ///
1222
+ /// buff
1223
+ /// });
1224
+ ///
1225
+ /// tokio::select! {
1226
+ /// _ = child.wait() => {}
1227
+ /// _ = rx => { child.kill().await.expect("kill failed") },
1228
+ /// }
1229
+ ///
1230
+ /// let buff = read_stdout.await.unwrap();
1231
+ ///
1232
+ /// assert_eq!(buff, b"Hello World!\n");
1233
+ /// }
1234
+ /// ```
1193
1235
pub async fn kill ( & mut self ) -> io:: Result < ( ) > {
1194
1236
self . start_kill ( ) ?;
1195
1237
self . wait ( ) . await ?;
0 commit comments