From fc432df4bdf257416852e9c7bb4ba7a1ac6d83e1 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Fri, 16 Jul 2010 21:37:28 +0000 Subject: [PATCH] initial checkin of threadmanager class --- sfa/util/threadmanager.py | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 sfa/util/threadmanager.py diff --git a/sfa/util/threadmanager.py b/sfa/util/threadmanager.py new file mode 100755 index 00000000..3d5dd03e --- /dev/null +++ b/sfa/util/threadmanager.py @@ -0,0 +1,71 @@ +import threading +import time +from Queue import Queue + +def ThreadedMethod(callable, queue): + """ + A function decorator that returns a running thread. The thread + runs the specified callable and stores the result in the specified + results queue + """ + def wrapper(args, kwds): + class ThreadInstance(threading.Thread): + def run(self): + try: + queue.put(callable(*args, **kwds)) + except: + # ignore errors + pass + thread = ThreadInstance() + thread.start() + return thread + return wrapper + + + +class ThreadManager: + """ + ThreadManager executes a callable in a thread and stores the result + in a thread safe queue. + """ + queue = Queue() + threads = [] + + def run (self, method, *args, **kwds): + """ + Execute a callable in a separate thread. + """ + method = ThreadedMethod(method, self.queue) + thread = method(args, kwds) + self.threads.append(thread) + + start = run + + def get_results(self): + """ + Return a list of all the results so far. Blocks until + all threads are finished. + """ + for thread in self.threads: + thread.join() + results = [] + while not self.queue.empty(): + results.append(self.queue.get()) + return results + +if __name__ == '__main__': + + def f(name, n, sleep=1): + nums = [] + for i in range(n, n+5): + print "%s: %s" % (name, i) + nums.append(i) + time.sleep(sleep) + return nums + + threads = ThreadManager() + threads.run(f, "Thread1", 10, 2) + threads.run(f, "Thread2", -10, 1) + + results = threads.get_results() + print "Results:", results -- 2.43.0