From d401641268aca4d93fd63abebe480fc8fee8ec24 Mon Sep 17 00:00:00 2001 From: "Chris St. Pierre" Date: Fri, 15 Mar 2013 14:11:54 -0400 Subject: Executor: better timeout implementation --- src/lib/Bcfg2/Utils.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/lib/Bcfg2/Utils.py b/src/lib/Bcfg2/Utils.py index 3b1559528..7d7d26d5d 100644 --- a/src/lib/Bcfg2/Utils.py +++ b/src/lib/Bcfg2/Utils.py @@ -165,23 +165,19 @@ class Executor(object): self.logger = logging.getLogger(self.__class__.__name__) self.timeout = timeout - def _timeout_callback(self, proc): - """ Get a callback (suitable for passing to - :class:`threading.Timer`) that kills the given process. + def _timeout(self, proc): + """ A function suitable for passing to + :class:`threading.Timer` that kills the given process. :param proc: The process to kill upon timeout. :type proc: subprocess.Popen - :returns: function """ - def _timeout(): - """ Callback that kills ``proc`` """ - if proc.poll() == None: - try: - proc.kill() - self.logger.warning("Process exceeeded timeout, killing") - except OSError: - pass - - return _timeout + :returns: None """ + if proc.poll() == None: + try: + proc.kill() + self.logger.warning("Process exceeeded timeout, killing") + except OSError: + pass def run(self, command, inputdata=None, shell=False, timeout=None): """ Run a command, given as a list, optionally giving it the @@ -212,9 +208,7 @@ class Executor(object): if timeout is None: timeout = self.timeout if timeout is not None: - timer = threading.Timer(float(timeout), - self._timeout_callback(proc), - [proc]) + timer = threading.Timer(float(timeout), self._timeout, [proc]) timer.start() try: if inputdata: -- cgit v1.2.3-1-g7c22