-
Notifications
You must be signed in to change notification settings - Fork 0
/
TasksPool.py
54 lines (48 loc) · 1.88 KB
/
TasksPool.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
# encoding: utf-8
"""
This module allows you to queue and launch processes using PySide's QProcess
"""
from PySide.QtCore import QObject, QProcess, Signal, QEventLoop
from PySide.QtGui import QApplication
import logging
logger = logging.getLogger(__name__)
class TasksPool(QObject):
"""
This class allows you to add_task() for execution queue. Launch_list() starts the queue.
"""
task_done = Signal()
return_signal = Signal(str)
def __init__(self):
super(TasksPool, self).__init__()
self.tasks_pool = []
self.process = None
if not QApplication.instance():
self.qapp = QApplication([])
def add_task(self, command):
"""
:type command: str
:param command: A console command with arguments
Adds a task to the pool for later execution
"""
self.tasks_pool.append(command)
def __execute_task(self, task):
"""
:param task: Is a string used to start a process
"""
self.process = QProcess(self)
self.process.finished.connect(lambda *x: logger.debug(task + ' reports complete'))
self.process.finished.connect(lambda *x: self.return_signal.emit('►'+task+'◄reports complete'))
self.process.finished.connect(self.task_done)
# self.process.readyRead.connect(lambda *x: print(str(self.process.readAll())))
self.process.readyRead.connect(lambda *x: self.return_signal.emit(str(self.process.readAll())))
self.process.setProcessChannelMode(QProcess.MergedChannels)
self.process.start(task)
def launch_list(self):
"""
Starts the execution of the queue in consecutive order, waiting for previous process to finish
"""
for task in self.tasks_pool:
loop = QEventLoop()
self.task_done.connect(loop.quit)
self.__execute_task(task)
loop.exec_()