async_read on async_pipe子进程没有提供数据
async_read on async_pipe child process giving no data
我具有以下代码,从我的真实代码中简化了我试图在连接到子过程的async_pipe上进行async_read。在孩子过程中,我称为" LS"。作为一个测试,我希望我的异步阅读以获得结果。它返回以下
$ ./a.out
system:0
0
为什么这会发生我不知道?理想情况下,我想替换" LS"。在长期运行过程中,我可以在其中阅读ASYNC_READ。
一行。#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <boost/process.hpp>
namespace bp = boost::process;
class test {
private:
boost::asio::io_service ios;
boost::asio::io_service::work work;
bp::async_pipe ap;
std::vector<char> buf;
public:
test()
: ios(), work(ios), ap(ios) {
}
void read(
const boost::system::error_code& ec,
std::size_t size) {
std::cout << ec << std::endl;
std::cout << size << std::endl;
}
void run() {
bp::child c(bp::search_path("ls"), ".", bp::std_out > ap);
boost::asio::async_read(ap, boost::asio::buffer(buf),
boost::bind(&test::read,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
ios.run();
}
};
int main() {
test c;
c.run();
}
您阅读到大小为0的向量。
您读取0字节。那就是你要的。
我建议使用StreamBuf,然后阅读直到EOF。另外,删除work
,除非您确实希望run()
永远不会返回:
活在coliru
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/process.hpp>
#include <iostream>
namespace bp = boost::process;
class test {
private:
boost::asio::io_service ios;
bp::async_pipe ap;
boost::asio::streambuf buf;
public:
test() : ios(), ap(ios) {}
void read(const boost::system::error_code &ec, std::size_t size) {
std::cout << ec.message() << "n";
std::cout << size << "n";
std::cout << &buf << std::flush;
}
void run() {
bp::child c(bp::search_path("ls"), ".", bp::std_out > ap, ios);
async_read(ap, buf, boost::bind(&test::read, this, _1, _2));
ios.run();
}
};
int main() {
test c;
c.run();
}
打印,例如
End of file
15
a.out
main.cpp
相关文章:
- 提供对不同类型的数据(建议、代码审查)的线程安全访问的类
- 在 C++ 中,默认情况下构造函数为类的数据成员提供的值是多少?
- 访问与使用 TraceLoggingWrite 提供的事件关联的用户数据
- 引用类型的数据成员提供有关恒常正确性"loophole"
- 如果类在 C++ 中具有常量或引用类型的非静态数据成员,为什么编译器不提供默认赋值运算符?
- 为什么不能使用"( )"为类的非静态数据成员提供默认值?
- C++ QTextDocument::contentChange() 信号提供无效的输入数据
- 向不可变纹理存储提供数据
- 固定大小的容器,其中元素被排序,并可以为C 中的数据提供原始指针
- 是否有一种简单的方法可以从普通的 win32 C++代码向应用程序见解提供数据?
- 如何通过 COM3 提供数据
- cpp 中是否存在一种数据结构,可以轻松地提供一种基于已存在的实例构建新结构的方法
- 通过回调提供数据服务
- 直接提供数据而不是文件路径
- 使用Windows AES加密提供商在C 中解密C#加密数据
- 使用 C++ 从 Linux 串行端口读取会提供混乱的数据
- async_read on async_pipe子进程没有提供数据
- 是什么导致了 std::async 中的数据竞争
- 为什么C 标准专门为具有不同访问说明符的类数据成员的内存布局提供了余地
- Lua:为自定义用户数据提供一个字符串方法