我想保存ping命令的输出。请告诉我在哪里做错了?
let [res, pid, stdin, stdout, stderr] = GLib.spawn_async_with_pipes(null, ["ping","-c","1",host], null, GLib.SpawnFlags.SEARCH_PATH, null);
GLib.io_add_watch(stdout, GLib.IOCondition.IN, this._read, stdout);
答案 0 :(得分:1)
这是一个Glibmm示例,您应该能够轻松地将其转换为Python:
int child_pid;
int child_stdout;
Glib::spawn_async_with_pipes("", std::vector<std::string>{"ping", "192.168.1.1"}, Glib::SPAWN_SEARCH_PATH|Glib::SPAWN_STDERR_TO_DEV_NULL, sigc::slot<void>(), &child_pid, nullptr, &child_stdout);
Glib::RefPtr<Glib::IOChannel> ch_out = Glib::IOChannel::create_from_fd(child_stdout);
Glib::RefPtr<Glib::IOSource> src_out = ch_out->create_watch(Glib::IO_IN);
src_out->connect([ch_out](Glib::IOCondition cond){ return handle_stdout(cond, ch_out); });
src_out->attach(Glib::MainContext::get_default());
bool handle_stdout(Glib::IOCondition cond, Glib::RefPtr<Glib::IOChannel> ch) {
Glib::ustring text;
ch->read_line(text);
textview->get_buffer()->insert(textview->get_buffer()->end(), text);
return true;
}
编辑:完成阅读时,还应调用src_out->destroy()
将其从事件循环中删除。