- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经看到了 boost::process tutorial ...但是这个例子是一个单一的写入然后从子进程中读取一个单一的。我想知道是否有可能在子进程生命周期内让两个 async_pipes 都处于事件状态(读取和写入)。
我在从子->父管道(read_pipe
,在我的代码中)读取时遇到问题。我只能在关闭父->子管道 (write_pipe
) 后读取它,但这意味着我不会再在这个子进程上写任何东西,对吧?这是有道理的,但是有一些解决方法来维护双向 channel 吗?我的最终目标是在读取和写入 block 之间不断交替。
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <iostream>
#include <string>
#include <vector>
typedef std::function<void(const boost::system::error_code & ec, std::size_t n)> Handler;
int main(){
std::vector<char> vread_buffer(4096);
boost::asio::io_service ios;
boost::process::async_pipe read_pipe{ios};
boost::process::async_pipe write_pipe{ios};
auto child_process = boost::process::child( "stockfish_12_win_x64/stockfish_20090216_x64.exe" ,
boost::process::std_in < write_pipe ,
boost::process::std_out > read_pipe );
Handler on_stdout,on_stdin;
std::string read_string;
on_stdout = [&](const boost::system::error_code & ec, size_t n){
std::cout << "I'm reading " << n << " characters from the child process. Can't wait for more!" << std::endl;
read_string.reserve( read_string.size() + n );
read_string.insert( read_string.end() , vread_buffer.begin() , vread_buffer.begin() + n );
if(!ec) boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
};
on_stdin = [&]( const boost::system::error_code & ec, std::size_t n ){
std::cout << "I know that " << n << " characters were sent to the process. Yay! " << std::endl;
};
// {...} Suppose an undefined amount of time has passed
// Expected: To see the initial print from the program calling the on_stdout
// ... but it doesn't call anything.
boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
ios.poll();
ios.restart();
// Expected: To see the "on_stdin" handler being called... and it was!
std::string write_string = "uci\n";
boost::asio::async_write( write_pipe , boost::asio::buffer(write_string) , on_stdin );
ios.poll();
ios.restart();
// Sending a async_read will never do anything unless I close the write_pipe.
// How can I tell the io_service that last write was done so I can read again?
boost::asio::async_read( read_pipe , boost::asio::buffer(vread_buffer) , on_stdout );
ios.poll();
ios.restart();
}
最佳答案
我想我明白了。我应该使用 boost::asio::async_read_until
传递定界符 (char/regex/string) 而不是 boost::asio::async_read
这样,boost::process::buffer
应该改为 boost::asio::dynamic_buffer
std::string read_string;
auto d_buffer = boost::asio::dynamic_buffer(read_string);
// First read
boost::asio::async_read_until( read_pipe , d_buffer , '\n' , on_stdout );
ios.poll();
ios.restart();
// Any amount of writes can be done here with async_write
// Second read
d_buffer.consume(d_buffer.size(); // Will append new content into read_string anyway, this is just to read_string doesn't grow indefinitely. Also, I noted unexpected behavior if not consume all content (new content appended at random middle points)
boost::asio::async_read_until( read_pipe , d_buffer , '\n' , on_stdout );
ios.poll();
ios.restart();
关于c++ - 如何有效地使用 boost::process::async_pipe 进行写入和读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66148021/
我有以下代码,它是从我的真实代码中简化而来的,我试图在连接到子进程的 async_pipe 上执行 async_read。在子进程中,我调用“ls”。作为一个测试,我希望我的异步读取能够得到结果。它返
我已经看到了 boost::process tutorial ...但是这个例子是一个单一的写入然后从子进程中读取一个单一的。我想知道是否有可能在子进程生命周期内让两个 async_pipes 都处于
注意:我在本文中交替使用“客户端”和“子”这两个词来指代从“服务器”启动的进程。 我正在使用 boost::process::async_pipe 编写我使用 boost::process::chil
我是一名优秀的程序员,十分优秀!