From 305215a4356ec25dc996e46a94117d9481a0fb66 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 8 Feb 2022 12:27:23 +0800 Subject: [PATCH] bugfix --- appPublic/thread_workers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/appPublic/thread_workers.py b/appPublic/thread_workers.py index 7f240fb..84ccc49 100644 --- a/appPublic/thread_workers.py +++ b/appPublic/thread_workers.py @@ -4,17 +4,17 @@ import random from appPublic.background import Background class ThreadWorkers: - def __init__(self, worker_cnt=10): - self.semaphore = threading.Semaphore(value=worker_cnt) + def __init__(self, max_workers=10): + self.semaphore = threading.Semaphore(value=max_workers) self.co_worker = 0 def _do(self, func, *args, **kwargs): + self.semaphore.acquire() self.co_worker += 1 func(*args, **kwargs) self.co_worker -= 1 self.semaphore.release() def do(self, func, *args, **kwargs): - self.semaphore.acquire() b = Background(self._do, func, *args, **kwargs) b.start() @@ -27,7 +27,7 @@ if __name__ == '__main__': print('current workers=',worker.get_workers(), 'sleep=', t) time.sleep(t) - w = ThreadWorkers() + w = ThreadWorkers(max_workers=30) for i in range(100000): w.do(k, w)