From 6a0c2b58d1b16a200278593127839db895f2d2f3 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Tue, 27 Jul 2021 01:12:49 +0800 Subject: [PATCH] bugfix --- appPublic/thread_workers.py | 33 +++++++++++++++++++++++++++++++++ appPublic/workers.py | 23 ++++------------------- 2 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 appPublic/thread_workers.py diff --git a/appPublic/thread_workers.py b/appPublic/thread_workers.py new file mode 100644 index 0000000..8dd3b7a --- /dev/null +++ b/appPublic/thread_workers.py @@ -0,0 +1,33 @@ +import time +import threading +import random +from appPublic.background import Background + +class ThreadWorkers: + 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 + func(*args, **kwargs) + 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 = ThreadWorkers() + for i in range(100000): + w.do(k, w) + diff --git a/appPublic/workers.py b/appPublic/workers.py index d84ffa1..4099197 100644 --- a/appPublic/workers.py +++ b/appPublic/workers.py @@ -1,9 +1,9 @@ import os import sys import time -import pyodbc as db from multiprocessing import Process, Queue,Pipe,freeze_support,cpu_count from threading import Thread +import threading import logging from appPublic.folderUtils import ProgramPath @@ -95,25 +95,9 @@ class Worker(Process): return None class Workers(Thread): - def __init__(self,worker_cnt,Wklass,argv): + def __init__(self,worker_cnt): Thread.__init__(self) - self.worker_cnt = worker_cnt - self.aborted = False - self.workers = [] - self.taskQ = Queue() - self.doneQ = Queue() - self.follows = [] - self.task_cnt = 0 - self.resp_cnt = 0 - self.task_no = 0 - self.max_task_no_resp = 0 - - i = 0 - while i < self.worker_cnt: - w = Wklass(self.taskQ,self.doneQ,*argv) - self.workers.append(w) - w.start() - i += 1 + self sema = threading.Semaphore(value=worker_cnt) def __del__(self): self.cleanTaskQ() @@ -248,3 +232,4 @@ class Workers(Thread): except Exception as e: logger.info("thread %s error:%s" % (self.name,str(e))) return None,None,None,None +