I've made some more progression on this.
INITIAL CODE UPDATE following on from bugmenot's help:
The below code works, and as far as I can tell this is the simplest possible way to run two tools at the same time in PyQGIS
Critically, the functions which are used in the QgsTask
definition must have a parameter called task
from qgis.core import *
def one(task):
processing.run("native:slope", {'INPUT':'...Dem.tif','Z_FACTOR':1,'OUTPUT':'...Slope1.tif'})
def two(task):
processing.run("native:slope", {'INPUT':'...Dem.tif','Z_FACTOR':2,'OUTPUT':'...Slope2.tif'})
task1 = QgsTask.fromFunction('ItWorks', one)
task2 = QgsTask.fromFunction('ItWorks2', two)
task1.addSubTask(task2)
QgsApplication.taskManager().addTask(task1)
Please note that the script does not wait for addTask
to complete, and simply powers on, hence why more advanced setups have handling to figure out when the task has completed
I've also attempted to try and run several tools in sequence, though with a set up such that I want one task to not start until after two other tasks have completed
def firstProcess(task):
processing.run("qgis:rastercalculator", ...)
time.sleep(0.1)
def secondProcess(task):
processing.run("qgis:rastercalculator", ...)
time.sleep(0.1)
firstProcessTask = QgsTask.fromFunction('FirstProcess', firstProcess)
secondProcessTask = QgsTask.fromFunction('SecondProcess', secondProcess)
firstProcessTask.addSubTask(secondProcessTask)
QgsApplication.taskManager().addTask(firstProcessTask)
####################################
def thirdProcess(task):
try:
firstProcessTask.waitForFinished(timeout = 20000000)
except BaseException as e:
print("...")
try:
secondProcessTask.waitForFinished(timeout = 20000000)
except BaseException as e:
print("...")
processing.run("qgis:rastercalculator", ...)
thirdProcessTask = QgsTask.fromFunction('ThirdProcess', thirdProcess)
QgsApplication.taskManager().addTask(thirdProcessTask)
It's a little clunky, but it's the best I've been able to do given that if two tasks are run together through addSubTask
they appear to trigger task.waitForFinished
independently
If anyone has any improvements that would be great