]> git.decadent.org.uk Git - dak.git/blobdiff - daklib/threadpool.py
Improve file headers in threadpool.py and cruft.py.
[dak.git] / daklib / threadpool.py
index 95a14a34de99218c97c2754418970d64fdb34a77..f1b68ce2e9bb4088222e2b0227a9793fafadafcd 100644 (file)
@@ -1,33 +1,41 @@
-import threading
-from time import sleep
+"""
+thread pool implementation for Python
+
+@contact: Debian FTPMaster <ftpmaster@debian.org>
+@copyright: 2003 Tim Lesher
+@copyright: 2004 Carl Kleffner
+@copyright: 2010, 2011 Torsten Werner <twerner@debian.org>
+"""
 
 # This code is a modified copy of
 # http://code.activestate.com/recipes/203871-a-generic-programming-thread-pool/
 # and is licensed under the Python License. The full text of the license
 # is available in the file COPYING-PSF.
 
-# FIXME:
-# numThreads defaults to 16 in __init__ to work best on
-# franck.debian.org but the default value should be specified in
-# dak.conf
+import threading
+from time import sleep
+
+from daklib.config import Config
 
-# Ensure booleans exist (not needed for Python 2.2.1 or higher)
-try:
-    True
-except NameError:
-    False = 0
-    True = not False
+if Config().has_key('Common::ThreadCount'):
+    defaultThreadCount = int(Config()['Common::ThreadCount'])
+else:
+    defaultThreadCount = 1
 
 class ThreadPool:
 
     """Flexible thread pool class.  Creates a pool of threads, then
-    accepts tasks that will be dispatched to the next available
-    thread."""
+    accepts tasks that will be dispatched to the next available thread.
+    The argument numThreads defaults to 'Common::ThreadCount' which must
+    be specified in dak.conf."""
 
-    def __init__(self, numThreads = 16):
+    def __init__(self, numThreads = 0):
 
         """Initialize the thread pool with numThreads workers."""
 
+        if numThreads == 0:
+            numThreads = defaultThreadCount
+
         self.__threads = []
         self.__resizeLock = threading.Condition(threading.Lock())
         self.__taskLock = threading.Condition(threading.Lock())
@@ -125,14 +133,15 @@ class ThreadPool:
         # Tell all the threads to quit
         self.__resizeLock.acquire()
         try:
-            self.__setThreadCountNolock(0)
-            self.__isJoining = True
-
             # Wait until all threads have exited
             if waitForThreads:
+                for t in self.__threads:
+                    t.goAway()
                 for t in self.__threads:
                     t.join()
                     del t
+            self.__setThreadCountNolock(0)
+            self.__isJoining = True
 
             # Reset the pool for potential reuse
             self.__isJoining = False