ori.subprocess

Utility function for running external commands as subprocesses.

ori.subprocess.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.

Parameters
  • command (Sequence[Union[bytes, str, 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.

  • stdout_function (Optional[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 print() or, say, logging.debug().

  • stderr_function (Optional[Callable]) – This is a function that your subprocess will call for every line of standard error that your program emits. The same rules apply as for stdout_function.

  • exception_function (Optional[Callable]) – This is a function to call if we catch an exception while logging the program.

  • exit_code_on_killed (int) – This is the exit code we return for the subprocess when we catch an exception while logging the program.

Returns

This returns an already-started multiprocessing.Process instance, which you can use to monitor or kill the process as time goes on.

Warning

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.