-
Notifications
You must be signed in to change notification settings - Fork 5
/
appWorkerStack.py
49 lines (35 loc) · 1.52 KB
/
appWorkerStack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from PyQt6 import QtCore
from appWorker import Worker
class WorkerStack(QtCore.QObject):
worker_task = QtCore.pyqtSignal(dict) # 'worker_name', 'func', 'params'
thread_exception = QtCore.pyqtSignal(object)
def __init__(self, workers_number):
super(WorkerStack, self).__init__()
self.workers = []
self.threads = []
self.load = {} # {'worker_name': tasks_count}
# Create workers crew
for i in range(0, workers_number):
worker = Worker(self, 'Slogger-' + str(i))
thread = QtCore.QThread()
worker.moveToThread(thread)
# worker.connect(thread, QtCore.SIGNAL("started()"), worker.run)
thread.started.connect(worker.run)
worker.task_completed.connect(self.on_task_completed)
thread.start(QtCore.QThread.Priority.NormalPriority)
self.workers.append(worker)
self.threads.append(thread)
self.load[worker.name] = 0
def __del__(self):
for thread in self.threads:
thread.terminate()
def add_task(self, task):
worker_name = min(self.load, key=self.load.get)
self.load[worker_name] += 1
self.worker_task.emit({'worker_name': worker_name, 'fcn': task['fcn'], 'params': task['params']})
def on_task_completed(self, worker_name):
self.load[str(worker_name)] -= 1
def quit(self):
for thread in self.threads:
thread.quit()
thread.wait()