From 4b245fce7366fbd38d4fe812873c3a0e7594f29e Mon Sep 17 00:00:00 2001 From: Josh Durgin Date: Wed, 22 Jun 2011 10:50:09 -0700 Subject: [PATCH] Add a utility for running functions in parallel. --- teuthology/parallel.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 teuthology/parallel.py diff --git a/teuthology/parallel.py b/teuthology/parallel.py new file mode 100644 index 00000000000..1e5b02245c9 --- /dev/null +++ b/teuthology/parallel.py @@ -0,0 +1,27 @@ +import gevent +import logging + +log = logging.getLogger(__name__) + +def run(func, all_args): + tasks = [] + for args in all_args: + assert isinstance(args, list), 'args must be a list of lists' + tasks.append(gevent.spawn(func, *args)) + gevent.sleep(0) + + try: + while tasks: + task = tasks[-1] + try: + task.get(block=True, timeout=5) + except gevent.Timeout: + continue + else: + tasks.pop() + except: + log.exception('Exception from parallel greenlet') + for task in tasks: + log.error('KILLING TASK') + task.kill(block=True) + raise