bugfix
This commit is contained in:
parent
39b93323ad
commit
6a0c2b58d1
33
appPublic/thread_workers.py
Normal file
33
appPublic/thread_workers.py
Normal file
@ -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)
|
||||||
|
|
@ -1,9 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import pyodbc as db
|
|
||||||
from multiprocessing import Process, Queue,Pipe,freeze_support,cpu_count
|
from multiprocessing import Process, Queue,Pipe,freeze_support,cpu_count
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
import threading
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from appPublic.folderUtils import ProgramPath
|
from appPublic.folderUtils import ProgramPath
|
||||||
@ -95,25 +95,9 @@ class Worker(Process):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
class Workers(Thread):
|
class Workers(Thread):
|
||||||
def __init__(self,worker_cnt,Wklass,argv):
|
def __init__(self,worker_cnt):
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
self.worker_cnt = worker_cnt
|
self sema = threading.Semaphore(value=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
|
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.cleanTaskQ()
|
self.cleanTaskQ()
|
||||||
@ -248,3 +232,4 @@ class Workers(Thread):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info("thread %s error:%s" % (self.name,str(e)))
|
logger.info("thread %s error:%s" % (self.name,str(e)))
|
||||||
return None,None,None,None
|
return None,None,None,None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user