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
Caveat Emptor!
Most mozprocess development happened before Python 3; some key features of mozprocess were intended to work around bugs and inadequacies in Python subprocess which are no longer a concern today. Today we know that mozprocess has many bugs of its own, and the module has struggled to find modern ownership: THIS MODULE IS POORLY MAINTAINED.
At the same time, mozprocess has many clients and provides a convenient solution to some complex process handling needs. Our best advice:
for routine process handling needs, use Python subprocess – NOT mozprocess
for convenient process handling with optional, simple output timeouts and/or output handling, use mozprocess.run_and_wait(); this is the simplest, most isolated, and most modern mozprocess interface
for more complex needs, use mozprocess ProcessHandlerMixin/ProcessHandler but proceed with caution!
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 instantiated 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.
proc_sleep_echo.sh:
#!/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:
proc_sleep_echo.bat:
@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.
Execution
Status
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())
Timeout
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(…, 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.
Waiting
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()
Killing
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 initialization 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()
Child management
Consider the following scripts:
proc_child.sh:
#!/bin/sh
for i in a b c d e
do
echo $i
sleep 1
done
proc_parent.sh:
#!/bin/sh
./proc_child.sh
for i in 1 2 3 4 5
do
echo $i
sleep 1
done
For windows users consider:
proc_child.bat:
@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
)
proc_parent.bat:
@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.
Ignoring 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 stopping (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()
API Documentation
- mozprocess.run_and_wait(cwd=None, env=None, text=True, timeout=None, timeout_handler=None, output_timeout=None, output_timeout_handler=None, output_line_handler=None)
Run a process and wait for it to complete, with optional support for line-by-line output handling and timeouts.
On timeout or output timeout, the callback should kill the process; many clients use mozcrash.kill_and_get_minidump() in the timeout callback.
run_and_wait is not intended to be a generic replacement for subprocess. Clients requiring different options or behavior should use subprocess directly.
- Parameters:
args – command to run. May be a string or a list.
cwd – working directory for command.
env – environment to use for the process (defaults to os.environ).
text – open streams in text mode if True; else use binary mode.
timeout – seconds to wait for process to complete before calling timeout_handler
timeout_handler – function to be called if timeout reached
output_timeout – seconds to wait for process to generate output
output_timeout_handler – function to be called if output_timeout is reached
output_line_handler – function to be called for every line of process output
- class mozprocess.ProcessHandlerMixin(cmd, args=None, cwd=None, env=None, ignore_children=False, kill_on_timeout=True, processOutputLine=(), processStderrLine=(), onTimeout=(), onFinish=(), **kwargs)
A class for launching and manipulating local processes.
- Parameters:
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 an empty list).
processStderrLine – function or list of functions to be called for each line of error output - stderr - produced by the process (defaults to an empty list). If this is not specified, stderr lines will be sent to the processOutputLine callbacks.
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.
- __init__(cmd, args=None, cwd=None, env=None, ignore_children=False, kill_on_timeout=True, processOutputLine=(), processStderrLine=(), onTimeout=(), onFinish=(), **kwargs)
- property commandline
the string value of the command line (command + args)
- kill(sig=None, timeout=None)
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)
- 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.
- property timedOut
True if the process has timed out for any reason.
- wait(timeout=None)
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=True, storeOutput=True, **kwargs)
Convenience class for handling processes with default output handlers.
By default, all output is sent to stdout. This can be disabled by setting the stream argument to None.
If processOutputLine keyword argument is specified 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 then if stream is True (the default).
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.