diff options
author | mbligh <mbligh@592f7852-d20e-0410-864c-8624ca9c26a4> | 2008-01-17 16:26:39 +0000 |
---|---|---|
committer | mbligh <mbligh@592f7852-d20e-0410-864c-8624ca9c26a4> | 2008-01-17 16:26:39 +0000 |
commit | c3aee0ffed098e73e1258c2fa8a44b5e9dd5cb32 (patch) | |
tree | 5ed04be620fb3a0eace44ce7b2a53aa7b04098c5 /server/subcommand.py | |
parent | ec75576c431a843bd19b37c0e2eca824bb3b54f6 (diff) | |
download | autotest-c3aee0ffed098e73e1258c2fa8a44b5e9dd5cb32.tar.gz |
Added timeout to parallel subcommands.
Tested by running parallel tests for the following situations:
without timeout specified
with timeout specified and timing out
with timeout specified and not timing out
Signed-off-by: Colby Ranger <cranger@google.com>
git-svn-id: http://test.kernel.org/svn/autotest/trunk@1176 592f7852-d20e-0410-864c-8624ca9c26a4
Diffstat (limited to 'server/subcommand.py')
-rw-r--r-- | server/subcommand.py | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/server/subcommand.py b/server/subcommand.py index dbeb8b6844..b1e1b27309 100644 --- a/server/subcommand.py +++ b/server/subcommand.py @@ -1,24 +1,31 @@ __author__ = """Copyright Andy Whitcroft, Martin J. Bligh - 2006, 2007""" -import sys, os, subprocess, traceback +import sys, os, subprocess, traceback, time, utils from common.error import * -def parallel(tasklist): +def parallel(tasklist, timeout=None): """Run an set of predefined subcommands in parallel""" pids = [] error = False for task in tasklist: task.fork_start() + + remaining_timeout = None + if timeout: + endtime = time.time() + timeout + for task in tasklist: - status = task.fork_waitfor() + if timeout: + remaining_timeout = max(endtime - time.time(), 1) + status = task.fork_waitfor(remaining_timeout) if status != 0: error = True if error: raise AutoservError('One or more subcommands failed') -def parallel_simple(function, arglist, log=True): +def parallel_simple(function, arglist, log=True, timeout=None): """Each element in the arglist used to create a subcommand object, where that arg is used both as a subdir name, and a single argument to pass to "function". @@ -36,9 +43,9 @@ def parallel_simple(function, arglist, log=True): if log: subdir = str(arg) else: - subdir = None + subdir = None subcommands.append(subcommand(function, args, subdir)) - parallel(subcommands) + parallel(subcommands, timeout) def _where_art_thy_filehandles(): @@ -154,8 +161,25 @@ class subcommand: os._exit(0) - def fork_waitfor(self): - (pid, status) = os.waitpid(self.pid, 0) + def fork_waitfor(self, timeout=None): + if not timeout: + (pid, status) = os.waitpid(self.pid, 0) + else: + pid = None + start_time = time.time() + while time.time() <= start_time + timeout: + (pid, status) = os.waitpid(self.pid, os.WNOHANG) + if pid: + break + time.sleep(1) + + if not pid: + utils.nuke_pid(self.pid) + print "subcommand failed pid %d" % self.pid + print "%s(%s)" % (self.func, self.args) + print "timeout after %ds" % timeout + print + return None if status != 0: print "subcommand failed pid %d" % pid |