Question

I want to save the output of ping command. Please tell where am I doing it wrong ?

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);

Update

Quoting this from one of my replies as this adds to the problem statement.

Actually I want to create an applet for Cinnamon Desktop (Linux Mint 16) using Javascript which would ping a host and display the average ping time in the panel as its label. So I was using GLib.spawn_async_with_pipes to do the same. So please guide me through this.

Was it helpful?

Solution

Here is a Glibmm example, you should be able to translate it easily to 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;
}

Edit: One should also call src_out->destroy() when done reading to remove it from the event loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top