Gio.Subprocess¶
| Subclasses: | None |
|---|
Methods¶
| Inherited: | GObject.Object (37), Gio.Initable (2) |
|---|---|
| Structs: | GObject.ObjectClass (5) |
| class | new (argv, flags) |
communicate (stdin_buf, cancellable) |
|
communicate_async (stdin_buf, cancellable, callback, *user_data) |
|
communicate_finish (result) |
|
communicate_utf8 (stdin_buf, cancellable) |
|
communicate_utf8_async (stdin_buf, cancellable, callback, *user_data) |
|
communicate_utf8_finish (result) |
|
force_exit () |
|
get_exit_status () |
|
get_identifier () |
|
get_if_exited () |
|
get_if_signaled () |
|
get_status () |
|
get_stderr_pipe () |
|
get_stdin_pipe () |
|
get_stdout_pipe () |
|
get_successful () |
|
get_term_sig () |
|
send_signal (signal_num) |
|
wait (cancellable) |
|
wait_async (cancellable, callback, *user_data) |
|
wait_check (cancellable) |
|
wait_check_async (cancellable, callback, *user_data) |
|
wait_check_finish (result) |
|
wait_finish (result) |
Virtual Methods¶
| Inherited: | GObject.Object (7), Gio.Initable (1) |
|---|
Properties¶
| Name | Type | Flags | Short Description |
|---|---|---|---|
argv |
[str] |
w/co | Argument vector |
flags |
Gio.SubprocessFlags |
w/co | Subprocess flags |
Signals¶
| Inherited: | GObject.Object (1) |
|---|
Fields¶
| Inherited: | GObject.Object (1) |
|---|
Class Details¶
-
class
Gio.Subprocess(**kwargs)¶ Bases: GObject.Object,Gio.InitableAbstract: No Gio.Subprocessallows the creation of and interaction with child processes.Processes can be communicated with using standard GIO-style APIs (ie:
Gio.InputStream,Gio.OutputStream). 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
Gio.OutputStream.splice_async(). This makesGio.Subprocesssignificantly more powerful and flexible than equivalent APIs in some other languages such as thesubprocess.pyincluded with Python. For example, usingGio.Subprocessone 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
Gio.Subprocess.communicate() API is provided similar to thecommunicate()method ofsubprocess.py. This enables very easy interaction with a subprocess that has been opened with pipes.Gio.Subprocessdefaults 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 theGio.SubprocessAPI (unlessGio.SubprocessFlags.INHERIT_FDSwas specified).Gio.Subprocesswill quickly reap all child processes as they exit, avoiding “zombie processes” remaining around for long periods of time.Gio.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,
Gio.Subprocesshas 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.Gio.Subprocessattempts 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). SeeGio.Subprocess.new(). TheGio.SubprocessLauncherAPI 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
Gio.Subprocesswill involve callingGio.Subprocess.new(), followed byGio.Subprocess.wait_async() orGio.Subprocess.wait(). After the process exits, the status can be checked using functions such asGio.Subprocess.get_if_exited() (which are similar to the familiar WIFEXITED-style POSIX macros).New in version 2.40.
-
classmethod
new(argv, flags)[source]¶ Parameters: - argv ([
str]) – commandline arguments for the subprocess - flags (
Gio.SubprocessFlags) – flags that define the behaviour of the subprocess
Raises: Returns: A newly created
Gio.Subprocess, orNoneon error (and error will be set)Return type: Create a new process with the given flags and argument list.
The argument list is expected to be
None-terminated.New in version 2.40.
- argv ([
-
communicate(stdin_buf, cancellable)[source]¶ Parameters: - stdin_buf (
GLib.BytesorNone) – data to send to the stdin of the subprocess, orNone - cancellable (
Gio.CancellableorNone) – aGio.Cancellable
Raises: Returns: Trueif successfulstdout_buf: data read from the subprocess stdout stderr_buf: data read from the subprocess stderr Return type: (
bool, stdout_buf:GLib.BytesorNone, stderr_buf:GLib.BytesorNone)Communicate with the subprocess until it terminates, and all input and output has been completed.
If stdin_buf is given, the subprocess must have been created with
Gio.SubprocessFlags.STDIN_PIPE. The given data is fed to the stdin of the subprocess and the pipe is closed (ie: EOF).At the same time (as not to cause blocking when dealing with large amounts of data), if
Gio.SubprocessFlags.STDOUT_PIPEorGio.SubprocessFlags.STDERR_PIPEwere used, reads from those streams. The data that was read is returned in stdout and/or the stderr.If the subprocess was created with
Gio.SubprocessFlags.STDOUT_PIPE, stdout_buf will contain the data read from stdout. Otherwise, for subprocesses not created withGio.SubprocessFlags.STDOUT_PIPE, stdout_buf will be set toNone. Similar provisions apply to stderr_buf andGio.SubprocessFlags.STDERR_PIPE.As usual, any output variable may be given as
Noneto ignore it.If you desire the stdout and stderr data to be interleaved, create the subprocess with
Gio.SubprocessFlags.STDOUT_PIPEandGio.SubprocessFlags.STDERR_MERGE. The merged result will be returned in stdout_buf and stderr_buf will be set toNone.In case of any error (including cancellation),
Falsewill be returned with error set. Some or all of the stdin data may have been written. Any stdout or stderr data that has been read will be discarded. None of the out variables (aside from error) will have been set to anything in particular and should not be inspected.In the case that
Trueis returned, the subprocess has exited and the exit status inspection APIs (eg:Gio.Subprocess.get_if_exited(),Gio.Subprocess.get_exit_status()) may be used.You should not attempt to use any of the subprocess pipes after starting this function, since they may be left in strange states, even if the operation was cancelled. You should especially not attempt to interact with the pipes while the operation is in progress (either from another thread or if using the asynchronous version).
New in version 2.40.
- stdin_buf (
-
communicate_async(stdin_buf, cancellable, callback, *user_data)[source]¶ Parameters: - stdin_buf (
GLib.BytesorNone) – Input data, orNone - cancellable (
Gio.CancellableorNone) – Cancellable - callback (
Gio.AsyncReadyCallbackorNone) – Callback - user_data (
objectorNone) – User data
Asynchronous version of
Gio.Subprocess.communicate(). Complete invocation withGio.Subprocess.communicate_finish().- stdin_buf (
-
communicate_finish(result)[source]¶ Parameters: result ( Gio.AsyncResult) – ResultRaises: GLib.ErrorReturns: stdout_buf: Return location for stdout data stderr_buf: Return location for stderr data Return type: ( bool, stdout_buf:GLib.BytesorNone, stderr_buf:GLib.BytesorNone)Complete an invocation of
Gio.Subprocess.communicate_async().
-
communicate_utf8(stdin_buf, cancellable)[source]¶ Parameters: - stdin_buf (
strorNone) – data to send to the stdin of the subprocess, orNone - cancellable (
Gio.CancellableorNone) – aGio.Cancellable
Raises: Returns: stdout_buf: data read from the subprocess stdout stderr_buf: data read from the subprocess stderr Return type: Like
Gio.Subprocess.communicate(), but validates the output of the process as UTF-8, and returns it as a regular NUL terminated string.On error, stdout_buf and stderr_buf will be set to undefined values and should not be used.
- stdin_buf (
-
communicate_utf8_async(stdin_buf, cancellable, callback, *user_data)[source]¶ Parameters: - stdin_buf (
strorNone) – Input data, orNone - cancellable (
Gio.CancellableorNone) – Cancellable - callback (
Gio.AsyncReadyCallbackorNone) – Callback - user_data (
objectorNone) – User data
Asynchronous version of
Gio.Subprocess.communicate_utf8(). Complete invocation withGio.Subprocess.communicate_utf8_finish().- stdin_buf (
-
communicate_utf8_finish(result)[source]¶ Parameters: result ( Gio.AsyncResult) – ResultRaises: GLib.ErrorReturns: stdout_buf: Return location for stdout data stderr_buf: Return location for stderr data Return type: ( bool, stdout_buf:strorNone, stderr_buf:strorNone)Complete an invocation of
Gio.Subprocess.communicate_utf8_async().
-
force_exit()[source]¶ 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
Gio.Subprocess.wait() to monitor the status of the process after calling this function.On Unix, this function sends %SIGKILL.
New in version 2.40.
-
get_exit_status()[source]¶ Returns: the exit status Return type: intCheck 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.
This is equivalent to the system WEXITSTATUS macro.
It is an error to call this function before
Gio.Subprocess.wait() and unlessGio.Subprocess.get_if_exited() returnedTrue.New in version 2.40.
-
get_identifier()[source]¶ Returns: the subprocess identifier, or Noneif the subprocess has terminatedReturn type: strorNoneOn 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
None.New in version 2.40.
-
get_if_exited()[source]¶ Returns: Trueif the case of a normal exitReturn type: boolCheck if the given subprocess exited normally (ie: by way of exit() or return from main()).
This is equivalent to the system WIFEXITED macro.
It is an error to call this function before
Gio.Subprocess.wait() has returned.New in version 2.40.
-
get_if_signaled()[source]¶ Returns: Trueif the case of termination due to a signalReturn type: boolCheck if the given subprocess terminated in response to a signal.
This is equivalent to the system WIFSIGNALED macro.
It is an error to call this function before
Gio.Subprocess.wait() has returned.New in version 2.40.
-
get_status()[source]¶ Returns: the (meaningless) waitpid() exit status from the kernel Return type: intGets the raw status code of the process, as from waitpid().
This value has no particular meaning, but it can be used with the macros defined by the system headers such as WIFEXITED. It can also be used with
GLib.spawn_check_exit_status().It is more likely that you want to use
Gio.Subprocess.get_if_exited() followed byGio.Subprocess.get_exit_status().It is an error to call this function before
Gio.Subprocess.wait() has returned.New in version 2.40.
-
get_stderr_pipe()[source]¶ Returns: the stderr pipe Return type: Gio.InputStreamGets the
Gio.InputStreamfrom which to read the stderr output of self.The process must have been created with
Gio.SubprocessFlags.STDERR_PIPE.New in version 2.40.
-
get_stdin_pipe()[source]¶ Returns: the stdout pipe Return type: Gio.OutputStreamGets the
Gio.OutputStreamthat you can write to in order to give data to the stdin of self.The process must have been created with
Gio.SubprocessFlags.STDIN_PIPE.New in version 2.40.
-
get_stdout_pipe()[source]¶ Returns: the stdout pipe Return type: Gio.InputStreamGets the
Gio.InputStreamfrom which to read the stdout output of self.The process must have been created with
Gio.SubprocessFlags.STDOUT_PIPE.New in version 2.40.
-
get_successful()[source]¶ Returns: Trueif the process exited cleanly with a exit status of 0Return type: boolChecks 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().
It is an error to call this function before
Gio.Subprocess.wait() has returned.New in version 2.40.
-
get_term_sig()[source]¶ Returns: the signal causing termination Return type: intGet the signal number that caused the subprocess to terminate, given that it terminated due to a signal.
This is equivalent to the system WTERMSIG macro.
It is an error to call this function before
Gio.Subprocess.wait() and unlessGio.Subprocess.get_if_signaled() returnedTrue.New in version 2.40.
-
send_signal(signal_num)[source]¶ Parameters: signal_num ( int) – the signal number to sendSends the UNIX signal signal_num to the subprocess, if it is still running.
This API is race-free. If the subprocess has terminated, it will not be signalled.
This API is not available on Windows.
New in version 2.40.
-
wait(cancellable)[source]¶ Parameters: cancellable ( Gio.CancellableorNone) – aGio.CancellableRaises: GLib.ErrorReturns: Trueon success,Falseif cancellable was cancelledReturn type: boolSynchronously wait for the subprocess to terminate.
After the process terminates you can query its exit status with functions such as
Gio.Subprocess.get_if_exited() andGio.Subprocess.get_exit_status().This function does not fail in the case of the subprocess having abnormal termination. See
Gio.Subprocess.wait_check() for that.Cancelling cancellable doesn’t kill the subprocess. Call
Gio.Subprocess.force_exit() if it is desirable.New in version 2.40.
-
wait_async(cancellable, callback, *user_data)[source]¶ Parameters: - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone - callback (
Gio.AsyncReadyCallbackorNone) – aGio.AsyncReadyCallbackto call when the operation is complete - user_data (
objectorNone) – user_data for callback
Wait for the subprocess to terminate.
This is the asynchronous version of
Gio.Subprocess.wait().New in version 2.40.
- cancellable (
-
wait_check(cancellable)[source]¶ Parameters: cancellable ( Gio.CancellableorNone) – aGio.CancellableRaises: GLib.ErrorReturns: Trueon success,Falseif process exited abnormally, or cancellable was cancelledReturn type: boolCombines
Gio.Subprocess.wait() withGLib.spawn_check_exit_status().New in version 2.40.
-
wait_check_async(cancellable, callback, *user_data)[source]¶ Parameters: - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone - callback (
Gio.AsyncReadyCallbackorNone) – aGio.AsyncReadyCallbackto call when the operation is complete - user_data (
objectorNone) – user_data for callback
Combines
Gio.Subprocess.wait_async() withGLib.spawn_check_exit_status().This is the asynchronous version of
Gio.Subprocess.wait_check().New in version 2.40.
- cancellable (
-
wait_check_finish(result)[source]¶ Parameters: result ( Gio.AsyncResult) – theGio.AsyncResultpassed to yourGio.AsyncReadyCallbackRaises: GLib.ErrorReturns: Trueif successful, orFalsewith error setReturn type: boolCollects the result of a previous call to
Gio.Subprocess.wait_check_async().New in version 2.40.
-
wait_finish(result)[source]¶ Parameters: result ( Gio.AsyncResult) – theGio.AsyncResultpassed to yourGio.AsyncReadyCallbackRaises: GLib.ErrorReturns: Trueif successful, orFalsewith error setReturn type: boolCollects the result of a previous call to
Gio.Subprocess.wait_async().New in version 2.40.
-
classmethod
Property Details¶
-
Gio.Subprocess.props.argv¶ Name: argvType: [ str]Default Value: []Flags: WRITABLE,CONSTRUCT_ONLYArgument vector
-
Gio.Subprocess.props.flags¶ Name: flagsType: Gio.SubprocessFlagsDefault Value: Gio.SubprocessFlags.NONEFlags: WRITABLE,CONSTRUCT_ONLYSubprocess flags