Subprocess

gio.subprocess.Subprocess allows the creation of and interaction with child processes.

Processes can be communicated with using standard GIO-style APIs (ie: gio.input_stream.InputStream, gio.output_stream.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.output_stream.OutputStream.spliceAsync. This makes gio.subprocess.Subprocess significantly more powerful and flexible than equivalent APIs in some other languages such as the subprocess.py included with Python. For example, using gio.subprocess.Subprocess 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 gio.subprocess.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.

gio.subprocess.Subprocess 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 gio.subprocess.Subprocess API (unless gio.types.SubprocessFlags.InheritFds was specified).

gio.subprocess.Subprocess will quickly reap all child processes as they exit, avoiding ‘zombie processes’ remaining around for long periods of time. gio.subprocess.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.subprocess.Subprocess 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. By default, the PATH of the current process is used. You can specify gio.types.SubprocessFlags.SearchPathFromEnvp to use the PATH of the launcher environment instead.

gio.subprocess.Subprocess 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 gio.subprocess.Subprocess.new_. The gio.subprocess_launcher.SubprocessLauncher 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 gio.subprocess.Subprocess will involve calling gio.subprocess.Subprocess.new_, followed by gio.subprocess.Subprocess.waitAsync or gio.subprocess.Subprocess.wait. After the process exits, the status can be checked using functions such as gio.subprocess.Subprocess.getIfExited (which are similar to the familiar WIFEXITED-style POSIX macros).

class Subprocess : ObjectG , Initable {}

Members

Functions

communicate
bool communicate(glib.bytes.Bytes stdinBuf, gio.cancellable.Cancellable cancellable, glib.bytes.Bytes stdoutBuf, glib.bytes.Bytes stderrBuf)

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

communicateAsync
void communicateAsync(glib.bytes.Bytes stdinBuf, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Asynchronous version of gio.subprocess.Subprocess.communicate. Complete invocation with gio.subprocess.Subprocess.communicateFinish.

communicateFinish
bool communicateFinish(gio.async_result.AsyncResult result, glib.bytes.Bytes stdoutBuf, glib.bytes.Bytes stderrBuf)

Complete an invocation of gio.subprocess.Subprocess.communicateAsync.

communicateUtf8
bool communicateUtf8(string stdinBuf, gio.cancellable.Cancellable cancellable, string stdoutBuf, string stderrBuf)

Like gio.subprocess.Subprocess.communicate, but validates the output of the process as UTF-8, and returns it as a regular NUL terminated string.

communicateUtf8Async
void communicateUtf8Async(string stdinBuf, gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Asynchronous version of gio.subprocess.Subprocess.communicateUtf8. Complete invocation with gio.subprocess.Subprocess.communicateUtf8Finish.

communicateUtf8Finish
bool communicateUtf8Finish(gio.async_result.AsyncResult result, string stdoutBuf, string stderrBuf)

Complete an invocation of gio.subprocess.Subprocess.communicateUtf8Async.

forceExit
void forceExit()

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.Subprocess.wait to monitor the status of the process after calling this function.

getExitStatus
int getExitStatus()

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.

getIdentifier
string getIdentifier()

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.

getIfExited
bool getIfExited()

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

getIfSignaled
bool getIfSignaled()

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

getStatus
int getStatus()

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

getStderrPipe
gio.input_stream.InputStream getStderrPipe()

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

getStdinPipe
gio.output_stream.OutputStream getStdinPipe()

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

getStdoutPipe
gio.input_stream.InputStream getStdoutPipe()

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

getSuccessful
bool getSuccessful()

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().

getTermSig
int getTermSig()

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

sendSignal
void sendSignal(int signalNum)

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

wait
bool wait(gio.cancellable.Cancellable cancellable)

Synchronously wait for the subprocess to terminate.

waitAsync
void waitAsync(gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Wait for the subprocess to terminate.

waitCheck
bool waitCheck(gio.cancellable.Cancellable cancellable)

Combines gio.subprocess.Subprocess.wait with glib.global.spawnCheckWaitStatus.

waitCheckAsync
void waitCheckAsync(gio.cancellable.Cancellable cancellable, gio.types.AsyncReadyCallback callback)

Combines gio.subprocess.Subprocess.waitAsync with glib.global.spawnCheckWaitStatus.

waitCheckFinish
bool waitCheckFinish(gio.async_result.AsyncResult result)

Collects the result of a previous call to gio.subprocess.Subprocess.waitCheckAsync.

waitFinish
bool waitFinish(gio.async_result.AsyncResult result)

Collects the result of a previous call to gio.subprocess.Subprocess.waitAsync.

Static functions

new_
gio.subprocess.Subprocess new_(string[] argv, gio.types.SubprocessFlags flags)

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

Mixed In Members

From mixin InitableT!()

init_
bool init_(gio.cancellable.Cancellable cancellable)

Initializes the object implementing the interface.

Inherited Members

From ObjectG

setGObject
void setGObject(void* cObj, Flag!"Take" take)

Set the GObject of a D ObjectG wrapper.

cPtr
void* cPtr(Flag!"Dup" dup)

Get a pointer to the underlying C object.

ref_
void* ref_(void* gObj)

Calls g_object_ref() on a GObject.

unref
unref(void* gObj)

Calls g_object_unref() on a GObject.

getType
GType getType()

Get the GType of an object.

gType
GType gType [@property getter]

GObject GType property.

self
ObjectG self()

Convenience method to return this cast to a type. For use in D with statements.

getDObject
T getDObject(void* cptr, Flag!"Take" take)

Template to get the D object from a C GObject and cast it to the given D object type.

connectSignalClosure
ulong connectSignalClosure(string signalDetail, DClosure closure, Flag!"After" after)

Connect a D closure to an object signal.

setProperty
void setProperty(string propertyName, T val)

Template for setting a GObject property.

getProperty
T getProperty(string propertyName)

Template for getting a GObject property.

compatControl
size_t compatControl(size_t what, void* data)
bindProperty
gobject.binding.Binding bindProperty(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags)

Creates a binding between source_property on source and target_property on target.

bindPropertyFull
gobject.binding.Binding bindPropertyFull(string sourceProperty, gobject.object.ObjectG target, string targetProperty, gobject.types.BindingFlags flags, gobject.closure.Closure transformTo, gobject.closure.Closure transformFrom)

Creates a binding between source_property on source and target_property on target, allowing you to set the transformation functions to be used by the binding.

forceFloating
void forceFloating()

This function is intended for #GObject implementations to re-enforce a floating[floating-ref] object reference. Doing this is seldom required: all #GInitiallyUnowneds are created with a floating reference which usually just needs to be sunken by calling gobject.object.ObjectG.refSink.

freezeNotify
void freezeNotify()

Increases the freeze count on object. If the freeze count is non-zero, the emission of "notify" signals on object is stopped. The signals are queued until the freeze count is decreased to zero. Duplicate notifications are squashed so that at most one #GObject::notify signal is emitted for each property modified while the object is frozen.

getData
void* getData(string key)

Gets a named field from the objects table of associations (see gobject.object.ObjectG.setData).

getProperty
void getProperty(string propertyName, gobject.value.Value value)

Gets a property of an object.

getQdata
void* getQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata.

getv
void getv(string[] names, gobject.value.Value[] values)

Gets n_properties properties for an object. Obtained properties will be set to values. All properties must be valid. Warnings will be emitted and undefined behaviour may result if invalid properties are passed in.

isFloating
bool isFloating()

Checks whether object has a floating[floating-ref] reference.

notify
void notify(string propertyName)

Emits a "notify" signal for the property property_name on object.

notifyByPspec
void notifyByPspec(gobject.param_spec.ParamSpec pspec)

Emits a "notify" signal for the property specified by pspec on object.

refSink
gobject.object.ObjectG refSink()

Increase the reference count of object, and possibly remove the floating[floating-ref] reference, if object has a floating reference.

runDispose
void runDispose()

Releases all references to other objects. This can be used to break reference cycles.

setData
void setData(string key, void* data)

Each object carries around a table of associations from strings to pointers. This function lets you set an association.

setProperty
void setProperty(string propertyName, gobject.value.Value value)

Sets a property on an object.

stealData
void* stealData(string key)

Remove a specified datum from the object's data associations, without invoking the association's destroy handler.

stealQdata
void* stealQdata(glib.types.Quark quark)

This function gets back user data pointers stored via gobject.object.ObjectG.setQdata and removes the data from object without invoking its destroy() function (if any was set). Usually, calling this function is only required to update user data pointers with a destroy notifier, for example:

thawNotify
void thawNotify()

Reverts the effect of a previous call to gobject.object.ObjectG.freezeNotify. The freeze count is decreased on object and when it reaches zero, queued "notify" signals are emitted.

watchClosure
void watchClosure(gobject.closure.Closure closure)

This function essentially limits the life time of the closure to the life time of the object. That is, when the object is finalized, the closure is invalidated by calling gobject.closure.Closure.invalidate on it, in order to prevent invocations of the closure with a finalized (nonexisting) object. Also, gobject.object.ObjectG.ref_ and gobject.object.ObjectG.unref are added as marshal guards to the closure, to ensure that an extra reference count is held on object during invocation of the closure. Usually, this function will be called on closures that use this object as closure data.

connectNotify
ulong connectNotify(string detail, T callback, Flag!"After" after)

Connect to Notify signal.

From Initable

init_
bool init_(gio.cancellable.Cancellable cancellable)

Initializes the object implementing the interface.