如何创建在 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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...