diff options
author | Bert JW Regeer <bertjw@regeer.org> | 2019-04-02 21:51:06 -0600 |
---|---|---|
committer | Bert JW Regeer <bertjw@regeer.org> | 2019-04-02 21:51:06 -0600 |
commit | 80203e5b100fd691018d198b7b954127835ef629 (patch) | |
tree | 9f322a3b4b26067c8ab1e68593f409bb9b06074b | |
parent | c083d0b7a645fe75c6a0ab0393be844cf2caf046 (diff) | |
download | waitress-80203e5b100fd691018d198b7b954127835ef629.tar.gz |
Instead of iterating over dictionary, use integer
This avoids looping over the dictionary in `add_task` for the extra
complexity of keeping a simple counter of which threads are active or
not activate.
-rw-r--r-- | waitress/task.py | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/waitress/task.py b/waitress/task.py index a252de5..e6ed276 100644 --- a/waitress/task.py +++ b/waitress/task.py @@ -47,9 +47,6 @@ hop_by_hop = frozenset(( )) -ThreadIdle = 1 -ThreadBusy = 2 - class ThreadedTaskDispatcher(object): """A Task Dispatcher that creates a thread for each task. """ @@ -59,6 +56,8 @@ class ThreadedTaskDispatcher(object): def __init__(self): self.threads = {} # { thread number -> ThreadIdle or ThreadBusy } + self.active = 0 + self.thread_count = 0 self.queue = deque() self.queue_lock = threading.Condition(threading.Lock()) @@ -72,23 +71,25 @@ class ThreadedTaskDispatcher(object): try: while True: with self.queue_lock: + self.active += 1 while not self.queue and threads.get(thread_no): - threads[thread_no] = ThreadIdle + self.active -= 1 self.queue_lock.wait() + self.active += 1 if not threads.get(thread_no): break task = self.queue.popleft() if task is None: # Special value: kill this thread. break - threads[thread_no] = ThreadBusy try: task.service() - except Exception as e: + except Exception: self.logger.exception( - 'Exception when servicing %r' % task) + 'Exception when servicing %r', task) finally: with self.queue_lock: + self.active -= 1 self.stop_count -= 1 threads.pop(thread_no, None) @@ -101,7 +102,7 @@ class ThreadedTaskDispatcher(object): # Start threads. while thread_no in threads: thread_no = thread_no + 1 - threads[thread_no] = ThreadIdle + threads[thread_no] = 1 running += 1 self.start_new_thread(self.handler_thread, (thread_no,)) thread_no = thread_no + 1 @@ -113,19 +114,20 @@ class ThreadedTaskDispatcher(object): self.queue.append(None) running -= 1 self.queue_lock.notify(to_stop) + self.thread_count = count def add_task(self, task): try: task.defer() - except: + except Exception: task.cancel() raise with self.queue_lock: self.queue.append(task) self.queue_lock.notify() - if not any(x == ThreadIdle for x in self.threads.values()): + if self.active >= self.thread_count: self.queue_logger.warning( - "Task queue depth is %d" % + "Task queue depth is %d", len(self.queue)) def shutdown(self, cancel_pending=True, timeout=5): |