私は...これに対処するための最良の方法は、使用して同じ問題でありました非同期入出力。
残念ながら、ブーストドキュメント@http://www.boost.org/doc/libs/master/doc/html/boost_process/extend.html#boost_process.extend.asyncは間違っていました... これはすべてがシンプルに見えますが、バッファのサイズをあらかじめ決めておかなければならないことを示しています。
私の機能は、1つのバッファを送受信します(question/response0のようなやりとりはありません)。私はエラーチェックのためにstderrを使用しています。これはアプリケーションに必要でした。私は実行アプリデコーダ/エンコーダである
using tstring=basic_string<TCHAR>;
void Run(
const tstring& exeName;
const tstring& args,
const std::string& input,
std::string& output,
std::string& error
)
{
using namespace boost;
asio::io_service ios;
std::vector<char> vOut(128 << 10);
auto outBuffer{ asio::buffer(vOut) };
process::async_pipe pipeOut(ios);
std::function<void(const system::error_code & ec, std::size_t n)> onStdOut;
onStdOut = [&](const system::error_code & ec, size_t n)
{
output.reserve(output.size() + n);
output.insert(output.end(), vOut.begin(), vOut.begin() + n);
if (!ec)
{
asio::async_read(pipeOut, outBuffer, onStdOut);
}
};
std::vector<char> vErr(128 << 10);
auto errBuffer{ asio::buffer(vErr) };
process::async_pipe pipeErr(ios);
std::function<void(const system::error_code & ec, std::size_t n)> onStdErr;
onStdErr = [&](const system::error_code & ec, size_t n)
{
error.reserve(error.size() + n);
error.insert(error.end(), vErr.begin(), vErr.begin() + n);
if (!ec)
{
asio::async_read(pipeErr, errBuffer, onStdErr);
}
};
auto inBuffer{ asio::buffer(input) };
process::async_pipe pipeIn(ios);
process::child c(
exeName + _T(" ") + args,
process::std_out > pipeOut,
process::std_err > pipeErr,
process::std_in < pipeIn
);
asio::async_write(pipeIn, inBuffer,
[&](const system::error_code & ec, std::size_t n)
{
pipeIn.async_close();
});
asio::async_read(pipeOut, outBuffer, onStdOut);
asio::async_read(pipeErr, errBuffer, onStdErr);
ios.run();
c.wait();
}
`、私はプロセスにファイル全体を送信し、同時にこのように結果を受け取るなしファイルサイズ制限
;)(c.exit_code」呼び出しありません。
重要
ブースト1.64でバグ修正があり、Windowsにのみ影響します。明らかに、ファイルboost \ process \ detail \ windows \ async_pipeの
です。HPP: 参照:https://github.com/klemens-morgenstern/boost-process/issues/90
行79:
~async_pipe()
{
//fix
//if (_sink .native() != ::boost::detail::winapi::INVALID_HANDLE_VALUE_)
// ::boost::detail::winapi::CloseHandle(_sink.native());
//if (_source.native() != ::boost::detail::winapi::INVALID_HANDLE_VALUE_)
// ::boost::detail::winapi::CloseHandle(_source.native());
boost::system::error_code ec;
close(ec);
//fix
}
あなたがへリンクのページにさらに少し見下した場合は、標準入力*と*出力の両方のリダイレクトが表示されます。標準出力とエラーの両方をリダイレクトする方法を教えてください。 –
ありがとう、はい私はそれを見た。私は** while ** stdoutとstderrを待つために適切なwhileループが必要です。また、ループの仕組みを知りたい。上記の(1)のようにデータを失うのはなぜですか? –