From: Michael Goldish Subject: [KVM-AUTOTEST PATCH 02/28] KVM test: kvm_utils.py: add a convenience function to run functions in parallel Date: Mon, 27 Dec 2010 18:01:29 +0200 Message-ID: <1293465715-16599-2-git-send-email-mgoldish@redhat.com> References: <1293465715-16599-1-git-send-email-mgoldish@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: autotest@test.kernel.org, kvm@vger.kernel.org Return-path: In-Reply-To: <1293465715-16599-1-git-send-email-mgoldish@redhat.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: autotest-bounces@test.kernel.org Errors-To: autotest-bounces@test.kernel.org List-Id: kvm.vger.kernel.org Signed-off-by: Michael Goldish --- client/tests/kvm/kvm_utils.py | 24 +++++++++++++++++++++++- 1 files changed, 23 insertions(+), 1 deletions(-) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 5d79112..57c9951 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -1046,7 +1046,7 @@ def get_vendor_from_pci_id(pci_id): class Thread(threading.Thread): """ - Runs a test in the background thread. + Run a function in a background thread. """ def __init__(self, target, args=(), kwargs={}): """ @@ -1102,6 +1102,28 @@ class Thread(threading.Thread): self._retval = None +def parallel(targets): + """ + Run multiple functions in parallel. + + @param targets: A sequence of tuples or functions. If it's a sequence of + tuples, each tuple will be interpreted as (target, args, kwargs) or + (target, args) or (target,) depending on its length. If it's a + sequence of functions, the functions will be called without + arguments. + @return: A list of the values returned by the functions called. + """ + threads = [] + for target in targets: + if isinstance(target, tuple) or isinstance(target, list): + t = Thread(*target) + else: + t = Thread(target) + threads.append(t) + t.start() + return [t.join() for t in threads] + + class KvmLoggingConfig(logging_config.LoggingConfig): """ Used with the sole purpose of providing convenient logging setup -- 1.7.3.3