我已经尝试了提升异步 IO 的示例(http://www.boost.org/doc/libs/1_66_0/doc/html/boost_process/tutorial.html),但所有这些似乎都阻塞了主程序,直到子进程已退出。
再看一遍。异步 I/O下的所有示例应该可以帮助您选择适合您的方法。
我们是否需要在单独的线程中读取孩子的标准输出?有人可以提供一个例子,主程序可以同时做其他事情而不是阻止来自孩子的标准输出吗?
不,你不需要。尽管您可以并且取决于您要实现的目标,但这可能是最简单的事情。
同步
你没有告诉我们你想做什么,所以假设你只想打印输出:
Live On Coliru:
bp::child c("/bin/bash", std::vector<std::string> { "-c", "for a in {1..10}; do sleep 2; echo 'Hello World !'; done" });
c.wait();
那是同步的,所以你不能同时做工作
使用阅读器线程
这就像:
Live On Coliru:
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
namespace bp = boost::process;
int main() {
bp::ipstream output;
std::thread reader([&output] {
std::string line;
while (std::getline(output, line))
std::cout << "Received: '" << line << "'" << std::endl;
});
bp::child c("/bin/bash",
std::vector<std::string> { "-c", "for a in {1..10}; do sleep 2; echo 'Hello World ('$a')!'; done" },
bp::std_out > output);
while (c.running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(2793));
std::cout << "(main thread working)" << std::endl;
}
std::cout << "(done)" << std::endl;
c.wait();
output.pipe().close();
reader.join();
}
打印 ( Live On Coliru):
Received: 'Hello World (1)!'
(main thread working)
Received: 'Hello World (2)!'
(main thread working)
Received: 'Hello World (3)!'
Received: 'Hello World (4)!'
(main thread working)
Received: 'Hello World (5)!'
(main thread working)
Received: 'Hello World (6)!'
(main thread working)
Received: 'Hello World (7)!'
Received: 'Hello World (8)!'
(main thread working)
Received: 'Hello World (9)!'
(main thread working)
Received: 'Hello World (10)!'
(main thread working)
(done)
异步 IO
不使用线程(嗯,只是主线程),可能看起来像:
Live On Coliru
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/asio/high_resolution_timer.hpp>
#include <iostream>
#include <iomanip>
namespace bp = boost::process;
struct OtherWork {
using clock = std::chrono::high_resolution_clock;
OtherWork(boost::asio::io_context& io) : timer(io) { }
void start() {
timer.expires_at(clock::time_point::max());
loop();
}
void stop() {
timer.expires_at(clock::time_point::min());
}
private:
void loop() {
if (timer.expires_at() == clock::time_point::min()) {
std::cout << "(done)" << std::endl;
return;
}
timer.expires_from_now(std::chrono::milliseconds(2793));
timer.async_wait([=](boost::system::error_code ec) {
if (!ec) {
std::cout << "(other work in progress)" << std::endl;
start();
} else {
std::cout << "(" << ec.message() << ")" << std::endl;
}
});
}
boost::asio::high_resolution_timer timer;
};
int main() {
boost::asio::io_context io;
bp::async_pipe output(io);
OtherWork mainwork{io};
bp::child c("/bin/bash", std::vector<std::string> { "-c", "for a in {1..10}; do sleep 2; echo 'Hello World ('$a')!'; done" },
bp::std_out > output, io, bp::on_exit([&mainwork,&output](auto...) {
output.close();
mainwork.stop();
}));
std::function<void()> readloop = [&,buffer=std::array<char, 32>{}]() mutable {
output.async_read_some(bp::buffer(buffer), [&](boost::system::error_code ec, size_t transferred) {
if (transferred) {
std::cout << "Received: '";
while (transferred && buffer[transferred-1] == '\n') // strip newline(s)
--transferred;
std::cout.write(buffer.data(), transferred);
std::cout << "'" << std::endl;
}
if (ec)
std::cout << "Output pipe: " << ec.message() << std::endl;
else
readloop();
});
};
mainwork.start();
readloop();
io.run();
}
印刷Live On Coliru
Received: 'Hello World (1)!'
(other work in progress)
Received: 'Hello World (2)!'
(other work in progress)
Received: 'Hello World (3)!'
Received: 'Hello World (4)!'
(other work in progress)
Received: 'Hello World (5)!'
(other work in progress)
Received: 'Hello World (6)!'
(other work in progress)
Received: 'Hello World (7)!'
Received: 'Hello World (8)!'
(other work in progress)
Received: 'Hello World (9)!'
(other work in progress)
Received: 'Hello World (10)!'
Output pipe: End of file
Child exited with code=0(Success)
(Operation canceled)