Class

GioSubprocess

Description [src]

final class Gio.Subprocess : GObject.Object {
  /* No available fields */
}

GSubprocess allows the creation of and interaction with child processes.

Processes can be communicated with using standard GIO-style APIs (ie: GInputStream, GOutputStream). There are GIO-style APIs to wait for process termination (ie: cancellable and with an asynchronous variant).

There is an API to force a process to terminate, as well as a race-free API for sending UNIX signals to a subprocess.

One major advantage that GIO brings over the core GLib library is comprehensive API for asynchronous I/O, such g_output_stream_splice_async(). This makes GSubprocess significantly more powerful and flexible than equivalent APIs in some other languages such as the subprocess.py included with Python. For example, using GSubprocess one could create two child processes, reading standard output from the first, processing it, and writing to the input stream of the second, all without blocking the main loop.

A powerful g_subprocess_communicate() API is provided similar to the communicate() method of subprocess.py. This enables very easy interaction with a subprocess that has been opened with pipes.

GSubprocess defaults to tight control over the file descriptors open in the child process, avoiding dangling-fd issues that are caused by a simple fork()/exec(). The only open file descriptors in the spawned process are ones that were explicitly specified by the GSubprocess API (unless G_SUBPROCESS_FLAGS_INHERIT_FDS was specified).

GSubprocess will quickly reap all child processes as they exit, avoiding “zombie processes” remaining around for long periods of time. g_subprocess_wait() can be used to wait for this to happen, but it will happen even without the call being explicitly made.

As a matter of principle, GSubprocess has no API that accepts shell-style space-separated strings. It will, however, match the typical shell behaviour of searching the PATH for executables that do not contain a directory separator in their name.

GSubprocess attempts to have a very simple API for most uses (ie: spawning a subprocess with arguments and support for most typical kinds of input and output redirection). See g_subprocess_new(). The GSubprocessLauncher API is provided for more complicated cases (advanced types of redirection, environment variable manipulation, change of working directory, child setup functions, etc).

A typical use of GSubprocess will involve calling g_subprocess_new(), followed by g_subprocess_wait_async() or g_subprocess_wait(). After the process exits, the status can be checked using functions such as g_subprocess_get_if_exited() (which are similar to the familiar WIFEXITED-style POSIX macros).

Available since:2.40

Hierarchy

hierarchy this GSubprocess implements_0 GInitable this--implements_0 ancestor_0 GObject ancestor_0--this

Ancestors

Implements

Constructors

g_subprocess_new

Create a new process with the given flags and varargs argument list. By default, matching the g_spawn_async() defaults, the child’s stdin will be set to the system null device, and stdout/stderr will be inherited from the parent. You can use flags to control this behavior.

Available since: 2.40

g_subprocess_newv

Create a new process with the given flags and argument list.

Available since: 2.40

Instance methods

g_subprocess_communicate

Communicate with the subprocess until it terminates, and all input and output has been completed.

Available since: 2.40

g_subprocess_communicate_async

Asynchronous version of g_subprocess_communicate(). Complete invocation with g_subprocess_communicate_finish().

g_subprocess_communicate_finish

Complete an invocation of g_subprocess_communicate_async().

g_subprocess_communicate_utf8

Like g_subprocess_communicate(), but validates the output of the process as UTF-8, and returns it as a regular NUL terminated string.

g_subprocess_communicate_utf8_async

Asynchronous version of g_subprocess_communicate_utf8(). Complete invocation with g_subprocess_communicate_utf8_finish().

g_subprocess_communicate_utf8_finish

Complete an invocation of g_subprocess_communicate_utf8_async().

g_subprocess_force_exit

Use an operating-system specific method to attempt an immediate, forceful termination of the process. There is no mechanism to determine whether or not the request itself was successful; however, you can use g_subprocess_wait() to monitor the status of the process after calling this function.

Available since: 2.40

g_subprocess_get_exit_status

Check the exit status of the subprocess, given that it exited normally. This is the value passed to the exit() system call or the return value from main.

Available since: 2.40

g_subprocess_get_identifier

On UNIX, returns the process ID as a decimal string. On Windows, returns the result of GetProcessId() also as a string. If the subprocess has terminated, this will return NULL.

Available since: 2.40

g_subprocess_get_if_exited

Check if the given subprocess exited normally (ie: by way of exit() or return from main()).

Available since: 2.40

g_subprocess_get_if_signaled

Check if the given subprocess terminated in response to a signal.

Available since: 2.40

g_subprocess_get_status

Gets the raw status code of the process, as from waitpid().

Available since: 2.40

g_subprocess_get_stderr_pipe

Gets the GInputStream from which to read the stderr output of subprocess.

Available since: 2.40

g_subprocess_get_stdin_pipe

Gets the GOutputStream that you can write to in order to give data to the stdin of subprocess.

Available since: 2.40

g_subprocess_get_stdout_pipe

Gets the GInputStream from which to read the stdout output of subprocess.

Available since: 2.40

g_subprocess_get_successful

Checks if the process was “successful”. A process is considered successful if it exited cleanly with an exit status of 0, either by way of the exit() system call or return from main().

Available since: 2.40

g_subprocess_get_term_sig

Get the signal number that caused the subprocess to terminate, given that it terminated due to a signal.

Available since: 2.40

g_subprocess_send_signal

Sends the UNIX signal signal_num to the subprocess, if it is still running.

Available since: 2.40

g_subprocess_wait

Synchronously wait for the subprocess to terminate.

Available since: 2.40

g_subprocess_wait_async

Wait for the subprocess to terminate.

Available since: 2.40

g_subprocess_wait_check

Combines g_subprocess_wait() with g_spawn_check_wait_status().

Available since: 2.40

g_subprocess_wait_check_async

Combines g_subprocess_wait_async() with g_spawn_check_wait_status().

Available since: 2.40

g_subprocess_wait_check_finish

Collects the result of a previous call to g_subprocess_wait_check_async().

Available since: 2.40

g_subprocess_wait_finish

Collects the result of a previous call to g_subprocess_wait_async().

Available since: 2.40

Methods inherited from GObject (43)
Methods inherited from GInitable (1)
g_initable_init

Initializes the object implementing the interface.

Available since: 2.22

Properties

Gio.Subprocess:argv
No description available.
Gio.Subprocess:flags
No description available.

Signals

Signals inherited from GObject (1)