-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskManager.py
74 lines (57 loc) · 2.31 KB
/
taskManager.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Queue
from PySide2 import QtCore, QtGui, QtWidgets
class TaskManager(QtCore.QObject):
messageChanged = QtCore.Signal(str)
numbersTaskRunningChanged = QtCore.Signal(int)
def __init__(self, parent=None):
super(TaskManager, self).__init__(parent)
self._max_task = 1
self._queue = Queue.Queue()
self._numbers_task_running = 0
self._running = False
def setMaxTask(self, max_task):
self._max_task = max_task
if self._running:
self.call_task()
def maxTask(self):
return self._max_task
def appendTask(self, task):
self._queue.put(task)
self.call_task()
def start(self):
self._running = True
self.call_task()
def stop(self):
self._running = False
def call_task(self):
if self._numbers_task_running < self.maxTask() and not self._queue.empty() and self._running:
cmd = self._queue.get()
process = QtCore.QProcess(self)
process.setProcessChannelMode(QtCore.QProcess.MergedChannels)
process.readyReadStandardOutput.connect(self.on_readyReadStandardOutput)
process.finished.connect(self.on_finished)
process.started.connect(self.on_started)
process.errorOccurred.connect(self.on_errorOccurred)
process.start(cmd)
def on_readyReadStandardOutput(self):
codec = QtCore.QTextCodec.codecForLocale()
decoder_stdout = codec.makeDecoder()
process = self.sender()
text = decoder_stdout.toUnicode(process.readAllStandardOutput())
self.messageChanged.emit(text)
def on_errorOccurred(self, error):
process = self.sender()
print("error: ", error, "-", " ".join([process.program()] + process.arguments()))
self.call_task()
def on_finished(self):
process = self.sender()
self._numbers_task_running -= 1
self.numbersTaskRunningChanged.emit(self._numbers_task_running)
self.call_task()
self.messageChanged.emit('Job Done!')
def on_started(self):
process = self.sender()
print("started: ", " ".join([process.program()] + process.arguments()))
self._numbers_task_running += 1
self.numbersTaskRunningChanged.emit(self._numbers_task_running)
self.call_task()