关闭 boost::process child 的标准输入
Close the stdin of boost::process child
我正在尝试使用 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();
问题是 child 可执行文件正在等待其标准输入 EOF。这里 child.wait() 无限期挂起......
我尝试使用 asio::buffer、std_in.close(),... 但没有成功。
我发现的唯一 hack 是 delete() inStream ……这并不可靠。
我应该如何 "notify" child 进程并使用新的 boost::process 库关闭其标准输入?
谢谢!
I tried to used 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(); });
});
其中 input
是
bp::async_pipe input(ios);
此外,检查进程是否真的在发送输出时卡住了!如果您未能使用输出,它将缓冲并在缓冲区已满时等待。
通过调用 inStream.close();
when you're done writing to it. You can also close it while launching with bp::std_in.close()
关闭管道。
asio 解决方案当然也可以避免死锁的危险。
我正在尝试使用 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();
问题是 child 可执行文件正在等待其标准输入 EOF。这里 child.wait() 无限期挂起......
我尝试使用 asio::buffer、std_in.close(),... 但没有成功。 我发现的唯一 hack 是 delete() inStream ……这并不可靠。
我应该如何 "notify" child 进程并使用新的 boost::process 库关闭其标准输入?
谢谢!
I tried to used 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(); });
});
其中 input
是
bp::async_pipe input(ios);
此外,检查进程是否真的在发送输出时卡住了!如果您未能使用输出,它将缓冲并在缓冲区已满时等待。
通过调用 inStream.close();
when you're done writing to it. You can also close it while launching with bp::std_in.close()
关闭管道。
asio 解决方案当然也可以避免死锁的危险。