Created
December 16, 2011 20:18
-
-
Save buzztaiki/1487781 to your computer and use it in GitHub Desktop.
gjs-io-sample.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GLib = imports.gi.GLib; | |
const Gio = imports.gi.Gio; | |
let [res, out, err, status] = GLib.spawn_command_line_sync('ls -la'); | |
print(out); | |
let [res, out] = GLib.spawn_command_line_sync('ls -la'); | |
print(out); | |
let [res, out] = GLib.spawn_sync(null, ['/bin/ls', '-la'], null, 0, null); | |
print(out); | |
let [res, out] = GLib.spawn_sync(GLib.getenv('HOME'), ['/bin/ls', '-la'], null, 0, null); | |
print(out); | |
let [res, out] = GLib.spawn_sync(null, ['ls', '-la'], null, GLib.SpawnFlags.SEARCH_PATH, null); | |
print(out); | |
GLib.spawn_command_line_async('ls -la'); | |
let [res, pid, in_fd, out_fd, err_fd] = GLib.spawn_async_with_pipes(null, ['/bin/cat'], null, 0, null); | |
let out_reader = new Gio.DataInputStream({ | |
base_stream: new Gio.UnixInputStream({fd: out_fd}) | |
}); | |
let in_writer = new Gio.DataOutputStream({ | |
base_stream: new Gio.UnixOutputStream({fd: in_fd}) | |
}); | |
let data = ["hoge", "fuga", ""].join("\n"); | |
in_writer.put_string(data, null); | |
let [out, size] = out_reader.read_line(null); | |
print(out); | |
let [out, size] = out_reader.read_line(null); | |
print(out); |
After a few iterations of calling spawn_async_with_pipes() I get the error 'Failed to create pipe for communicating with child process (Too many open files)' I tried closing everything I can, but I must be missing something.
With output:
let [result, stdout, stderr] = GLib.spawn_command_line_sync(command);
For those finding this in a search, there's a good example of a asynchronous file reader class here: https://github.com/optimisme/gjs-examples/blob/master/egSpawn.js
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When I developed a cinnamon applet it helped me to out.toString(), because out is not string!