mozprocess — Launch and manage processes¶
Mozprocess is a process-handling module that provides some additional features beyond those available with python’s subprocess:
- better handling of child processes, especially on Windows
- the ability to timeout the process after some absolute period, or some period without any data written to stdout/stderr
- the ability to specify output handlers that will be called for each line of output produced by the process
- the ability to specify handlers that will be called on process timeout and normal process termination
Running a process¶
mozprocess consists of two classes: ProcessHandler inherits from ProcessHandlerMixin.
Let’s see how to run a process. First, the class should be instanciated with at least one argument which is a command (or a list formed by the command followed by its arguments). Then the process can be launched using the run() method. Finally the wait() method will wait until end of execution.
from mozprocess import processhandler # under Windows replace by command = ['dir', '/a'] command = ['ls', '-l'] p = processhandler.ProcessHandler(command) print("execute command: %s" % p.commandline) p.run() p.wait()
Note that using ProcessHandler instead of ProcessHandlerMixin will print the output of executed command. The attribute commandline provides the launched command.
Collecting process output¶
Let’s now consider a basic shell script that will print numbers from 1 to 5 waiting 1 second between each. This script will be used as a command to launch in further examples.
#!/bin/sh for i in 1 2 3 4 5 do echo $i sleep 1 done
If you are running under Windows, you won’t be able to use the previous script (unless using Cygwin). So you’ll use the following script:
@echo off FOR %%A IN (1 2 3 4 5) DO ( ECHO %%A REM if you have TIMEOUT then use it instead of PING REM TIMEOUT /T 1 /NOBREAK PING -n 2 127.0.0.1 > NUL )
Mozprocess allows the specification of custom output handlers to gather process output while running. ProcessHandler will by default write all outputs on stdout. You can also provide (to ProcessHandler or ProcessHandlerMixin) a function or a list of functions that will be used as callbacks on each output line generated by the process.
In the following example the command’s output will be stored in a file output.log and printed in stdout:
import sys from mozprocess import processhandler fd = open('output.log', 'w') def tostdout(line): sys.stdout.write("<%s>\n" % line) def tofile(line): fd.write("<%s>\n" % line) # under Windows you'll replace by 'proc_sleep_echo.bat' command = './proc_sleep_echo.sh' outputs = [tostdout, tofile] p = processhandler.ProcessHandlerMixin(command, processOutputLine=outputs) p.run() p.wait() fd.close()
The process output can be saved (obj = ProcessHandler(..., storeOutput=True)) so as it is possible to request it (obj.output) at any time. Note that the default value for stroreOutput is True, so it is not necessary to provide it in the parameters.
import time import sys from mozprocess import processhandler command = './proc_sleep_echo.sh' # Windows: 'proc_sleep_echo.bat' p = processhandler.ProcessHandler(command, storeOutput=True) p.run() for i in xrange(10): print(p.output) time.sleep(0.5) p.wait()
In previous example, you will see the p.output list growing.
It is possible to query the status of the process via poll() that will return None if the process is still running, 0 if it ended without failures and a negative value if it was killed by a signal (Unix-only).
import time import signal from mozprocess import processhandler command = './proc_sleep_echo.sh' p = processhandler.ProcessHandler(command) p.run() time.sleep(2) print("poll status: %s" % p.poll()) time.sleep(1) p.kill(signal.SIGKILL) print("poll status: %s" % p.poll())
A timeout can be provided to the run() method. If the process last more than timeout seconds, it will be stopped.
After execution, the property timedOut will be set to True if a timeout was reached.
It is also possible to provide functions (obj = ProcessHandler[Mixin](..., onTimeout=functions)) that will be called if the timeout was reached.
from mozprocess import processhandler def ontimeout(): print("REACHED TIMEOUT") command = './proc_sleep_echo.sh' # Windows: 'proc_sleep_echo.bat' functions = [ontimeout] p = processhandler.ProcessHandler(command, onTimeout=functions) p.run(timeout=2) p.wait() print("timedOut = %s" % p.timedOut)
By default the process will be killed on timeout but it is possible to prevent this by setting kill_on_timeout to False.
p = processhandler.ProcessHandler(command, onTimeout=functions, kill_on_timeout=False) p.run(timeout=2) p.wait() print("timedOut = %s" % p.timedOut)
In this case, no output will be available after the timeout, but the process will still be running.
It is possible to wait until the process exits as already seen with the method wait(), or until the end of a timeout if given. Note that in last case the process is still alive after the timeout.
command = './proc_sleep_echo.sh' # Windows: 'proc_sleep_echo.bat' p = processhandler.ProcessHandler(command) p.run() p.wait(timeout=2) print("timedOut = %s" % p.timedOut) p.wait()
You can request to kill the process with the method kill. f the parameter “ignore_children” is set to False when the process handler class is initialized, all the process’s children will be killed as well.
Except on Windows, you can specify the signal with which to kill method the process (e.g.: kill(signal.SIGKILL)).
import time from mozprocess import processhandler command = './proc_sleep_echo.sh' # Windows: 'proc_sleep_echo.bat' p = processhandler.ProcessHandler(command) p.run() time.sleep(2) p.kill()
End of execution¶
You can provide a function or a list of functions to call at the end of the process using the initilization parameter onFinish.
from mozprocess import processhandler def finish(): print("Finished!!") command = './proc_sleep_echo.sh' # Windows: 'proc_sleep_echo.bat' p = processhandler.ProcessHandler(command, onFinish=finish) p.run() p.wait()
Consider the following scripts:
#!/bin/sh for i in a b c d e do echo $i sleep 1 done
#!/bin/sh ./proc_child.sh for i in 1 2 3 4 5 do echo $i sleep 1 done
For windows users consider:
@echo off FOR %%A IN (a b c d e) DO ( ECHO %%A REM TIMEOUT /T 1 /NOBREAK PING -n 2 127.0.0.1 > NUL )
@echo off call proc_child.bat FOR %%A IN (1 2 3 4 5) DO ( ECHO %%A REM TIMEOUT /T 1 /NOBREAK PING -n 2 127.0.0.1 > NUL )
For processes that launch other processes, mozprocess allows you to get child running status, wait for child termination, and kill children.
By default the ignore_children option is False. In that case, killing the main process will kill all its children at the same time.
import time from mozprocess import processhandler def finish(): print("Finished") command = './proc_parent.sh' p = processhandler.ProcessHandler(command, ignore_children=False, onFinish=finish) p.run() time.sleep(2) print("kill") p.kill()
If ignore_children is set to True, killing will apply only to the main process that will wait children end of execution before stoping (join).
import time from mozprocess import processhandler def finish(): print("Finished") command = './proc_parent.sh' p = processhandler.ProcessHandler(command, ignore_children=True, onFinish=finish) p.run() time.sleep(2) print("kill") p.kill()
- class mozprocess.ProcessHandlerMixin(cmd, args=None, cwd=None, env=None, ignore_children=False, kill_on_timeout=True, processOutputLine=(), onTimeout=(), onFinish=(), **kwargs)¶
A class for launching and manipulating local processes.
- cmd – command to run. May be a string or a list. If specified as a list, the first element will be interpreted as the command, and all additional elements will be interpreted as arguments to that command.
- args – list of arguments to pass to the command (defaults to None). Must not be set when cmd is specified as a list.
- cwd – working directory for command (defaults to None).
- env – is the environment to use for the process (defaults to os.environ).
- ignore_children – causes system to ignore child processes when True, defaults to False (which tracks child processes).
- kill_on_timeout – when True, the process will be killed when a timeout is reached. When False, the caller is responsible for killing the process. Failure to do so could cause a call to wait() to hang indefinitely. (Defaults to True.)
- processOutputLine – function or list of functions to be called for each line of output produced by the process (defaults to None).
- onTimeout – function or list of functions to be called when the process times out.
- onFinish – function or list of functions to be called when the process terminates normally without timing out.
- kwargs – additional keyword args to pass directly into Popen.
NOTE: Child processes will be tracked by default. If for any reason we are unable to track child processes and ignore_children is set to False, then we will fall back to only tracking the root process. The fallback will be logged.
the string value of the command line (command + args)
Kills the managed process.
If you created the process with ‘ignore_children=False’ (the default) then it will also also kill all child processes spawned by it. If you specified ‘ignore_children=True’ when creating the process, only the root process will be killed.
Note that this does not manage any state, save any output etc, it immediately kills the process.
Parameters: sig – Signal used to kill the process, defaults to SIGKILL (has no effect on Windows)
Called when a process finishes without a timeout.
Called when a process times out.
Called for each line of output that a process sends to stdout/stderr.
- run(timeout=None, outputTimeout=None)¶
Starts the process.
If timeout is not None, the process will be allowed to continue for that number of seconds before being killed. If the process is killed due to a timeout, the onTimeout handler will be called.
If outputTimeout is not None, the process will be allowed to continue for that number of seconds without producing any output before being killed.
True if the process has timed out.
Waits until all output has been read and the process is terminated.
If timeout is not None, will return after timeout seconds. This timeout only causes the wait function to return and does not kill the process.
Returns the process exit code value: - None if the process hasn’t terminated yet - A negative number if the process was killed by signal N (Unix only) - ‘0’ if the process ended without failures
- class mozprocess.ProcessHandler(cmd, logfile=None, stream=None, storeOutput=True, **kwargs)¶
Convenience class for handling processes with default output handlers.
If no processOutputLine keyword argument is specified, write all output to stdout. Otherwise, the function or the list of functions specified by this argument will be called for each line of output; the output will not be written to stdout automatically.
If storeOutput==True, the output produced by the process will be saved as self.output.
If logfile is not None, the output produced by the process will be appended to the given file.