关闭boost :: process子的stdin [英] Close the stdin of boost::process child
问题描述
我正在尝试使用Boost-1.64.0调用一个带有标准输入法的字符串的进程. 当前代码是:
I'm trying to call a process with a string to its stdin, with Boost-1.64.0. The current code is :
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()无限期地挂起……
The problem is that the child executable is waiting for its stdin EOF. Here child.wait() is hanging indefinitely…
我尝试使用asio :: buffer,std_in.close(),……但是没有运气. 我发现的唯一破解方法是delete()inStream…这并不是很可靠.
I tried to used asio::buffer, std_in.close(),… But no luck. The only hack I found was to delete() the inStream… And that's not really reliable.
我应该如何通知"子进程并使用新的boost :: process库关闭其stdin?
How am I supposed to "notify" the child process and close its stdin with the new boost::process library ?
谢谢!
推荐答案
我尝试使用asio :: buffer,
std_in.close()
这有效.当然,只有将其传递给启动函数(bp :: child构造函数,bp :: system等)时,它才起作用.
This works. Of course it only works if you pass it to a launch function (bp::child constructor, bp::system, etc).
如果需要传递数据,然后将其关闭,只需关闭关联的filedescriptor.我做这样的事情:
If you need to pass data, and then close it, simply close the associated filedescriptor. I do something like this:
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);
还要检查发送输出的过程实际上没有卡死!如果您无法使用输出,它将正在缓冲并等待缓冲区是否已满.
Also, check that the process is not actually stuck sending the output! If you fail to consume the output it would be buffering and waiting if the buffer is full.
这篇关于关闭boost :: process子的stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!