scriptharness.process module

Scriptharness multiprocessing support.

scriptharness.process.command_subprocess(queue, *args, **kwargs)

Run a subprocess as a multiprocess.Process. This will open STDOUT and STDERR to the same pipe, and read lines from it. Use this with watch_command() for timeout support.

Note

This is intended for non-binary output only.

Parameters:
  • queue (multiprocessing.Queue) – the queue to write to
  • *args – sent to subprocess.Popen
  • **kwargs – sent to subprocess.Popen
scriptharness.process.kill_proc_tree(pid, include_parent=False, wait=5)

Find the children of a process and kill them; optionally also kill the process. Uses psutil, which is cross-platform and py2&3 compatible.

From http://stackoverflow.com/a/4229404

Parameters:
  • pid (int) – The process ID of the parent.
  • include_parent (Optional[bool]) – kill the parent as well if True. Defaults to False.
  • wait (Optional[int]) – How long to wait for the children and parent to die. Defaults to 5.
scriptharness.process.kill_runner(runner)

Kill the runner process and children.

Parameters:runner (multiprocessing.Process) – the process to kill.
scriptharness.process.watch_command(logger, queue, runner, add_line_cb, max_timeout=None, output_timeout=None)

This function watches the queue of the command_subprocess process.

Usage:

queue = multiprocessing.Queue()
runner = multiprocessing.Process(target=command_subprocess,
                                 args=(queue,))
runner.start()
watch_command(logger, queue, runner, add_line_cb,
              output_timeout=output_timeout, max_timeout=max_timeout)
Parameters:
  • logger (logging.Logger) – the logger to use.
  • queue (multiprocessing.Queue) – the queue that the runner is writing to.
  • runner (multiprocessing.Process) – the runner Process to watch.
  • add_line_cb (function) – any output lines read will be sent here.
  • max_timeout (Optional[int]) – when specified, the process will be killed if it takes longer than this number of seconds. Default: None
  • output_timeout (Optional[int]) – when specified, the process will be killed if it doesn’t produce any output for this number of seconds. Default: None
Returns:

runner.exitcode – on non-timeout.

Return type:

int

Raises:
scriptharness.process.watch_output(logger, runner, stdout, stderr, max_timeout=None, output_timeout=None)

This function watches the queue of the output_subprocess process.

Usage:

runner = multiprocessing.Process(target=output_subprocess, args=(queue,))
runner.start()
watch_output(logger, runner, output_timeout=output_timeout,
             max_timeout=max_timeout)
Parameters:
  • logger (logging.Logger) – the logger to use.
  • runner (subprocess.Popen) – the runner process to watch.
  • max_timeout (Optional[int]) – when specified, the process will be killed if it takes longer than this number of seconds. Default: None
  • output_timeout (Optional[int]) – when specified, the process will be killed if it doesn’t produce any output for this number of seconds. Default: None
Returns:

runner.exitcode – on non-timeout.

Return type:

int

Raises: