From ed5976fe6b1bf6c9e7f07cde610c01835af314c2 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Fri, 22 Apr 2022 13:36:36 +0800 Subject: [PATCH] bugfix --- appPublic/process_workers.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100755 appPublic/process_workers.py diff --git a/appPublic/process_workers.py b/appPublic/process_workers.py new file mode 100755 index 0000000..0234290 --- /dev/null +++ b/appPublic/process_workers.py @@ -0,0 +1,36 @@ +import time +from multiprocessing import Process +import threading +import random +from appPublic.background import Background + +class ProcessWorkers: + def __init__(self, worker_cnt=10): + self.semaphore = threading.Semaphore(value=worker_cnt) + self.co_worker = 0 + def _do(self, func, *args, **kwargs): + self.semaphore.acquire() + self.co_worker += 1 + p = Process(target=func, args=args, kwargs=kwargs) + p.start() + p.join() + self.co_worker -= 1 + self.semaphore.release() + + def do(self, func, *args, **kwargs): + b = Background(self._do, func, *args, **kwargs) + b.start() + + def get_workers(self): + return self.co_worker + +if __name__ == '__main__': + def k(worker): + t = random.randint(1,4) + print('current workers=',worker.get_workers(), 'sleep=', t) + time.sleep(t) + + w = ProcessWorkers() + for i in range(100): + w.do(k, w) +