X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=daklib%2Fthreadpool.py;h=f075f5c380c8907959103b2d33ebe9d174270f30;hb=ec257c02a5d62fd27844c70814acd9616b24b4c8;hp=41ae34319a1c004155a20217454c97384fa396bf;hpb=dc1606c545145946238aec86045b38b3b14a722f;p=dak.git diff --git a/daklib/threadpool.py b/daklib/threadpool.py index 41ae3431..f075f5c3 100644 --- a/daklib/threadpool.py +++ b/daklib/threadpool.py @@ -15,6 +15,11 @@ 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 @@ -22,10 +27,13 @@ class ThreadPool: The argument numThreads defaults to 'Common::ThreadCount' which must be specified in dak.conf.""" - def __init__(self, numThreads = Config()['Common::ThreadCount']): + 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()) @@ -123,14 +131,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