From: Fredrik Tolf Date: Mon, 4 Apr 2016 00:13:03 +0000 (+0200) Subject: python3: Make pooled threads start exit when wait-pool is large for some time. X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;ds=inline;h=62220f392938f876514618b6742aed6e5555d6e1;p=ashd.git python3: Make pooled threads start exit when wait-pool is large for some time. --- 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: