From 32ed5bbcc38b9bc7a31ba2a25b253cec965091e1 Mon Sep 17 00:00:00 2001 From: Lucas Meneghel Rodrigues Date: Thu, 16 Oct 2014 12:35:07 +0200 Subject: [PATCH] avocado.utils.process: SubProcess() -> Add .start() and .stop() methods Now that we do not initialize the subprocess upon class initialization, add a utility method .start(), that will start running the command, useful for background commands. In order to make things symmetrical, add a .stop() method, that will send a sigterm to the process, then wait on it. Signed-off-by: Lucas Meneghel Rodrigues --- avocado/utils/process.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/avocado/utils/process.py b/avocado/utils/process.py index 52fe8b12..b29b4295 100644 --- a/avocado/utils/process.py +++ b/avocado/utils/process.py @@ -348,6 +348,19 @@ class SubProcess(object): self.result.stdout = self.get_stdout() self.result.stderr = self.get_stderr() + def start(self): + """ + Start running the subprocess. + + This method is particularly useful for background processes, since + you can start the subprocess and not block your test flow. + + :return: Subprocess PID. + :rtype: int + """ + self._init_subprocess() + return self._popen.pid + def get_stdout(self): """ Get the full stdout of the subprocess so far. @@ -417,14 +430,32 @@ class SubProcess(object): self._fill_results(rc) return rc + def stop(self): + """ + Stop background subprocess. + + Call this method to terminate the background subprocess and + wait for it results. + """ + self._init_subprocess() + if self.result.exit_status is None: + self.terminate() + return self.wait() + def run(self, timeout=None, sig=signal.SIGTERM): """ - Wait for the process to end, filling and returning the result attr. + Start a process and wait for it to end, returning the result attr. + + If the process was already started using .start(), this will simply + wait for it to end. :param timeout: Time (seconds) we'll wait until the process is finished. If it's not, we'll try to terminate it and get a status. :type timeout: float + :param sig: Signal to send to the process in case it did not end after + the specified timeout. + :type sig: int :returns: The command result object. :rtype: A :class:`avocado.utils.process.CmdResult` instance. """ -- GitLab