如何解决如何创建在 tokio::process::Child 退出而不关闭 stdin 后完成的未来
如何在不关闭 tokio::process::Child
的情况下创建在 stdin
终止时完成的未来。我知道有 try_wait
用于测试进程是否在没有关闭 stdin
的情况下终止,但我希望这种行为具有未来的语义。
我尝试为此问题准备 MRE,其中我的代码因在调用 stdin
后写入 wait
而出现混乱,但我观察到的与 tokio::process::Child
的文档中所述的行为不符{1}} 的 wait method。我希望看到 stdin.write_u8(24).await.unwrap();
行因管道损坏而崩溃,因为 stdin
应该已被 wait
关闭。
use tokio::{time,io::AsyncWriteExt}; // 1.0.1
use std::time::Duration;
#[tokio::main]
pub async fn main() {
let mut child = tokio::process::Command::new("nano")
.stdin(std::process::Stdio::piped())
.spawn()
.unwrap();
let mut stdin = child.stdin.take().unwrap();
let tasklet = tokio::spawn(async move {
child.wait().await
});
// this delay should give tokio::spawn plenty of time to spin up
// and call `wait` on the child (closing stdin)
time::sleep(Duration::from_millis(1000)).await;
// write character 24 (CANcel,^X) to stdin to close nano
stdin.write_u8(24).await.unwrap();
match tasklet.await {
Ok(exit_result) => match exit_result {
Ok(exit_status) => eprintln!("exit_status: {}",exit_status),Err(terminate_error) => eprintln!("terminate_error: {}",terminate_error)
}
Err(join_error) => eprintln!("join_error: {}",join_error)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。