From 62220f392938f876514618b6742aed6e5555d6e1 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Mon, 4 Apr 2016 02:13:03 +0200 Subject: [PATCH] python3: Make pooled threads start exit when wait-pool is large for some time. --- python3/ashd/serve.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/python3/ashd/serve.py b/python3/ashd/serve.py index 0eddc09..7811f66 100644 --- a/python3/ashd/serve.py +++ b/python3/ashd/serve.py @@ -204,6 +204,8 @@ class threadpool(handler): self.ccond = threading.Condition(self.clk) self.queue = collections.deque() self.waiting = set() + self.waitlimit = 5 + self.wlstart = 0.0 self.qlk = threading.Lock() self.qcond = threading.Condition(self.qlk) self.max = max @@ -275,9 +277,15 @@ class threadpool(handler): start = now = time.time() with self.qlk: while len(self.queue) < 1: + if len(self.waiting) >= self.waitlimit and now - self.wlstart >= timeout: + return self.waiting.add(th) - self.qcond.wait(start + timeout - now) - self.waiting.remove(th) + try: + if len(self.waiting) == self.waitlimit: + self.wlstart = now + self.qcond.wait(start + timeout - now) + finally: + self.waiting.remove(th) now = time.time() if now - start > timeout: return @@ -292,7 +300,7 @@ class threadpool(handler): def close(self): while True: - with self.lk: + with self.clk: if len(self.current) > 0: th = next(iter(self.current)) else: -- 2.11.0