关闭 boost::process 子进程的标准输入

2023-12-24

我正在尝试使用 Boost-1.64.0 调用一个带有字符串到其标准输入的进程。 当前的代码是:

  bp::opstream inStream ;
  bp::ipstream outStream;
  bp::ipstream errStream;

  bp::child child(
    command, // the command line
    bp::shell,
    bp::std_out > outStream,
    bp::std_err > errStream,
    bp::std_in  < inStream);


  // read the outStream/errStream in threads

  child.wait();

问题是子可执行文件正在等待其标准输入 EOF。这里 child.wait() 无限期地挂起......

我尝试使用 asio::buffer、std_in.close()...但没有运气。 我发现的唯一的黑客是删除()inStream...这并不真正可靠。

我应该如何“通知”子进程并使用新的 boost::process 库关闭其标准输入?

Thanks !


我尝试使用 asio::buffer,std_in.close()

这有效。当然,只有将其传递给启动函数(bp::child 构造函数、bp::system 等)时它才有效。

如果需要传递数据,然后关闭它,只需关闭关联的文件描述符即可。我做了这样的事情:

boost::asio::async_write(input, bp::buffer(_stdin_data), [&input](auto ec, auto bytes_written){
    if (ec) {
        logger.log(LOG_WARNING) << "Standard input rejected: " << ec.message() << " after " << bytes_written << " bytes written";
    }
    may_fail([&] { input.close(); });
});

Where input is

bp::async_pipe input(ios);

另外,检查进程是否实际上在发送输出时被卡住!如果您无法使用输出,那么如果缓冲区已满,它将进行缓冲并等待。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

关闭 boost::process 子进程的标准输入 的相关文章

随机推荐