Skip to content

Instantly share code, notes, and snippets.

@mvcsantos
Created June 3, 2015 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvcsantos/a66489d8f7030cc4504d to your computer and use it in GitHub Desktop.
Save mvcsantos/a66489d8f7030cc4504d to your computer and use it in GitHub Desktop.
Prototype of the multithreading implementation on QGIS toolbox
from PyQt4 import QtCore
from PyQt4.QtCore import pyqtSlot, qDebug
import time
import sys
class AlgorithmExecutor(QtCore.QObject):
instanceCounter = 0
started = QtCore.pyqtSignal(int)
progress = QtCore.pyqtSignal(int, int)
setResult = QtCore.pyqtSignal(int)
finished = QtCore.pyqtSignal(int)
def __init__(self, n_it, algorithm, parent = None):
QtCore.QObject.__init__(self, parent)
self.n = n_it
self.algorithm = algorithm
self.instance = AlgorithmExecutor.instanceCounter
AlgorithmExecutor.instanceCounter+=1
def run(self):
try:
for i in range(self.n):
#print '(instance ',self.instance,') it: ',i,'\n'
self.progress.emit(self.instance, i)
time.sleep(1)
self.setResult.emit(self.instance)
self.finished.emit(self.instance)
except:
e = sys.exc_info()[0]
qDebug("Signal not emitted!!")
self.finished.emit(self.instance)
print e
class TaskManager(QtCore.QObject):
finishApp = QtCore.pyqtSignal()
def __init__(self):
QtCore.QObject.__init__(self, None)
def setResult(self, i):
print "Setting result of instance ", i
def print_started_algorithm(self,i):
print "algorithm ",i, " started"
def print_finish_algorithm(self,i):
print "algorithm ",i, " finishes"
def print_progress(self, i, p):
print "(instance ",i ,") Progress: ",p
def connect_signal(self, algEx):
algEx.executed.connect(self.print_executed)
def increaseInstanceCounter(self):
AlgorithmExecutor.instanceCounter+=1
def decreaseInstanceCounter(self):
AlgorithmExecutor.instanceCounter-=1
if AlgorithmExecutor.instanceCounter == 0:
self.finishApp.emit()
def print_executed():
print "signal received - (Task Manager)"
if __name__ == "__main__":
app = QtCore.QCoreApplication([])
taskM = TaskManager()
objThread1 = QtCore.QThread()
algE1 = AlgorithmExecutor(3, "algorithm 1")
algE1.moveToThread(objThread1)
algE1.started.connect(taskM.print_started_algorithm)
algE1.progress.connect(taskM.print_progress)
algE1.setResult.connect(taskM.setResult)
algE1.finished.connect(objThread1.quit)
algE1.finished.connect(taskM.decreaseInstanceCounter)
objThread1.started.connect(algE1.run)
objThread1.started.connect(taskM.increaseInstanceCounter)
objThread1.start()
taskM.finishApp.connect(app.exit)
# -----------------
objThread2 = QtCore.QThread()
algE2 = AlgorithmExecutor(3, "algorithm 2")
algE2.moveToThread(objThread2)
algE2.started.connect(taskM.print_started_algorithm)
algE2.progress.connect(taskM.print_progress)
algE2.setResult.connect(taskM.setResult)
algE2.finished.connect(objThread2.quit)
algE2.finished.connect(taskM.decreaseInstanceCounter)
objThread2.started.connect(algE2.run)
objThread2.started.connect(taskM.increaseInstanceCounter)
objThread2.start()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment