diff mbox

[KVM-AUTOTEST,02/28] KVM test: kvm_utils.py: add a convenience function to run functions in parallel

Message ID 1293465715-16599-2-git-send-email-mgoldish@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Michael Goldish Dec. 27, 2010, 4:01 p.m. UTC
None
diff mbox

Patch

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