python-subprocess2

You can also import and use subprocess2 instead of the subprocess module. Doing a global extension allows your application or library to utilize the extensions without requiring modification of other codebases. In example, if you have a library that is opening a pipe and passing it to your application, it does not need to be modified for you to use the additional Popen methods on the object you received.

Simply installing the subprocess2 module does not do anything to the parent subprocess module, until it has been imported, and then only additions are made.

waitOrTerminate - Wait up to a certain number of seconds for the process to end.

If the process is running after the timeout has been exceeded, a SIGTERM will be sent.

Optionally, an additional SIGKILL can be sent after some configurable interval. See #terminateToKillSeconds doc below

@param timeoutSeconds <float> - Number of seconds to wait

@param pollInterval <float> (default .05)- Number of seconds between each poll

@param terminateToKillSeconds <float/None> (default 1.5) - If application does not end before #timeoutSeconds , terminate() will be called.

If this is set to None, an additional #pollInterval sleep will occur after calling .terminate, to allow the application to cleanup. returnCode will be return of app if finished, or None if did not complete.

If this is set to 0, no terminate signal will be sent, but directly to kill. Because the application cannot trap this, returnCode will be None.

If this is set to > 0, that number of seconds maximum will be given between .terminate and .kill. If the application does not terminate before KILL, returnCode will be None.

“actionTaken” is a mask of the SUBPROCESS2_PROCESS_* variables. If app completed normally, it will be SUBPROCESS2_PROCESS_COMPLETED, otherwise some mask of SUBPROCESS2_PROCESS_TERMINATED and/or SUBPROCESS2_PROCESS_KILLED

”’

Background Task Management

One of the benefits to modern computing is the ability to multitask. Your application may want to start several sub processes at once, and have them all collecting output simultaneously. The standard python “subprocess” module does not provide a simple approach through it’s API to do this.

subprocess2 extends the Popen module by adding the notion of a “Background Task.” When you call “runInBackground” on a pipe object, it will create and start a thread to automatically handle that process.

Calling “runInBackground” on a pipe returns a “BackgroundTaskInfo” option, which is dynamically updated as the status the subprocess progresses.

If you have open streams (stdout, stderr), they will automatically be read in non-blocking fashion into “stdoutData” and “stderrData” respectively on that object.

When the subprocess terminates, the “returnCode” field will be set, and “isFinished” will be marked True.

By default, data will be stored as bytes. To decode with a specific encoding (e.x. utf-8), pass the codec name as the “encoding” argument.

You can use this to farm out 10 processes quickly, collect all their data, and wait for them to complete. Other uses may be long-running associated proccesses, such as several searches collecting data, all being used to update a display.

Method Signature:

def runInBackground(self, pollInterval=.1, encoding=False):

”’

runInBackground - Create a background thread which will manage this process, automatically read from streams, and perform any cleanups

The object returned is a “BackgroundTaskInfo” object, and represents the state of the process. It is updated automatically as the program runs,

and if stdout or stderr are streams, they are automatically read from and populated into this object.

will have two processes running in the background, collecting their output automatically, and cleaning up automatically.

If you decide later you wait to block the current context until one of those pipes complete, you can pull it back into foreground (while maintaining the automatic population of streams/values) by calling “waitToFinish” on the BackgroundTaskInfo.