Utility function for running external commands as subprocesses.
run_process_in_background(*, command, stdout_function=None, stderr_function=None, exception_function=None, exit_code_on_killed=0)¶
Runs command, writing stdout and stderr to the logger in logger.
This function is based on this answer on StackOverflow.
PathLike]]) – This is an iterable–like a list–of strings. This iterable describes a command line program to run. For example, the command ls -l /home would be broken up into [“ls”, “-l”, “/home”]. You can use the function
shlex.split()to turn any string command into an iterable.
Callable]) – This is a function that your subprocess will call for every line of standard output that your program emits. You can choose functions like
int) – This is the exit code we return for the subprocess when we catch an exception while logging the program.
This returns an already-started
multiprocessing.Processinstance, which you can use to monitor or kill the process as time goes on.
The parameters stdout_function, stderr_function and exception_function are used to log the standard output, standard error, and any exceptions that your command might raise. These functions are run in a Python subprocess, which means that they must be importable at the module level and pickleable. Closures, which are functions enclosed in other functions, are not pickleable.
If you feel like you need to add additional parameters or state to your logging functions, you can write your functions as instances of Python classes with the __call__() magic method.