You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NOTE: that the output: is only printent after ALL processes finish, which I don't understand because the pipe should've been closed when each individual process ends. I used boost process 1.86 (latest)
#include<boost/process.hpp>
#include<future>
#include<spdlog/spdlog.h>namespacebp {
usingnamespaceboost::process;
}
intmain(int argc, char **argv) {
try {
auto run_child = [](const std::string &command) {
try {
bp::ipstream pipe_stream;
{
// Redirect stdout to our pipe
bp::child c(command, bp::std_out > pipe_stream);
c.wait();
SPDLOG_INFO("Finished process: {} ({})", command, c.exit_code());
} // child destructor runs here, closing its end of the pipe// Read the entire output from the pipe
std::string output;
std::string line;
while (std::getline(pipe_stream, line)) {
if (!output.empty()) output += "\n";
output += line;
}
SPDLOG_INFO("Output: {}", output);
returntrue;
} catch (const std::exception &e) {
SPDLOG_ERROR("Exception in child: {}", e.what());
returnfalse;
}
};
auto future0 = std::async(std::launch::async, run_child, "sleep 10.0");
auto future1 = std::async(std::launch::async, run_child, "false");
auto future2 = std::async(std::launch::async, run_child, "echo \"hello\"");
SPDLOG_INFO("Started both processed");
bool result0 = future0.get();
bool result1 = future1.get();
if (result0 && result1) {
SPDLOG_INFO("Both processes finished successfully");
} else {
SPDLOG_ERROR("One or both processes failed");
}
} catch (std::exception &e) {
SPDLOG_ERROR("Exception: {}", e.what());
}
return0;
}
The text was updated successfully, but these errors were encountered:
These are 3 individual processes, I expect the individual pipes to close when their associated process ends, but this only seems to happen after all 3 have ended. Where all the pipes are magically interlinked and unblock once the last process ends.
Note the log timestamps where the "output" lines are delayed
Oh right, you open the pipes first. All processes inherit the pipes by default. You need to pass limit_handles() or better yet switch to process::v2, which does that automatically.
Hey, maybe I'm doing something wrong but I can't get the pipes to read correctly with the following example:
The output is something like this:
NOTE: that the
output:
is only printent after ALL processes finish, which I don't understand because the pipe should've been closed when each individual process ends. I used boost process 1.86 (latest)The text was updated successfully, but these errors were encountered: