From patchwork Mon Dec 27 16:01:28 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 434321 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id oBRKCV9Q025997 for ; Mon, 27 Dec 2010 20:16:51 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754310Ab0L0QB5 (ORCPT ); Mon, 27 Dec 2010 11:01:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:61163 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754017Ab0L0QB5 (ORCPT ); Mon, 27 Dec 2010 11:01:57 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oBRG1uRf024023 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 27 Dec 2010 11:01:56 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id oBRG1tq9023866; Mon, 27 Dec 2010 11:01:55 -0500 Received: from moof.tlv.redhat.com (dhcp-1-185.tlv.redhat.com [10.35.1.185]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id oBRG1rFn017175; Mon, 27 Dec 2010 11:01:54 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish , Jason Wang Subject: [KVM-AUTOTEST PATCH 01/28] KVM test: introduce a helper class to run a function in the background Date: Mon, 27 Dec 2010 18:01:28 +0200 Message-Id: <1293465715-16599-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Mon, 27 Dec 2010 20:16:51 +0000 (UTC) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 5496e06..5d79112 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -5,7 +5,7 @@ KVM test utility functions. """ import time, string, random, socket, os, signal, re, logging, commands, cPickle -import fcntl, shelve, ConfigParser, rss_file_transfer +import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys from autotest_lib.client.bin import utils, os_dep from autotest_lib.client.common_lib import error, logging_config import kvm_subprocess @@ -1044,6 +1044,64 @@ def get_vendor_from_pci_id(pci_id): return re.sub(":", " ", commands.getoutput(cmd)) +class Thread(threading.Thread): + """ + Runs a test in the background thread. + """ + def __init__(self, target, args=(), kwargs={}): + """ + Initialize the instance. + + @param target: Function to run in the thread. + @param args: Arguments to pass to target. + @param kwargs: Keyword arguments to pass to target. + """ + threading.Thread.__init__(self) + self._target = target + self._args = args + self._kwargs = kwargs + + + def run(self): + """ + Run target (passed to the constructor). No point in calling this + function directly. Call start() to make this function run in a new + thread. + """ + self._e = None + self._retval = None + try: + try: + self._retval = self._target(*self._args, **self._kwargs) + except: + self._e = sys.exc_info() + raise + finally: + # Avoid circular references (start() may be called only once so + # it's OK to delete these) + del self._target, self._args, self._kwargs + + + def join(self, timeout=None): + """ + Join the thread. If target raised an exception, re-raise it. + Otherwise, return the value returned by target. + + @param timeout: Timeout value to pass to threading.Thread.join(). + """ + threading.Thread.join(self, timeout) + try: + if self._e: + raise self._e[0], self._e[1], self._e[2] + else: + return self._retval + finally: + # Avoid circular references (join() may be called multiple times + # so we can't delete these) + self._e = None + self._retval = None + + class KvmLoggingConfig(logging_config.LoggingConfig): """ Used with the sole purpose of providing convenient logging setup