This Processing Script is made available under the CC-0 license.
Wait for X seconds
"""
Model exported as python.
Name : template Wait for X seconds
Group :
With QGIS : 34300
"""
from typing import Any, Optional
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingContext
from qgis.core import QgsProcessingFeedback, QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterNumber
from qgis import processing
from time import time
class TemplateWaitForXSeconds(QgsProcessingAlgorithm):
def initAlgorithm(self, config: Optional[dict[str, Any]] = None):
self.addParameter(QgsProcessingParameterNumber('time', 'Time to wait in seconds', type=QgsProcessingParameterNumber.Double, minValue=0, defaultValue=1.5))
def processAlgorithm(self, parameters: dict[str, Any], context: QgsProcessingContext, model_feedback: QgsProcessingFeedback) -> dict[str, Any]:
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(100, model_feedback)
sleep_time = self.parameterAsDouble(parameters, 'time', context)
feedback.pushInfo(f"Waiting for {sleep_time} seconds...")
start_time = time()
elapsed_time = time() - start_time
while elapsed_time < sleep_time:
if feedback.isCanceled():
# In case user cancel
return {}
feedback.setCurrentStep(round(elapsed_time / sleep_time * 100))
elapsed_time = time() - start_time
feedback.pushInfo(f"End waiting")
results = {}
outputs = {}
return results
def name(self) -> str:
return 'waitforxseconds'
def displayName(self) -> str:
return 'Wait for X seconds'
def group(self) -> str:
return 'Extra modeler tools'
def tags(self):
return ["sleep","wait","delay", "suspend", "debug", "debugging"]
def shortHelpString(self):
help = ("This algorithm waits for a number of seconds effectively suspending the execution if used withing a model.\n\n"
"This is basically like the sleep function in python, but with a processing feedback around.\n\n"
"This is mainly useful in the context of the model designer to wait for some results, delays, simulate long operation, or for debugging purpose\n\n\n"
"Initially created to debug improvement to the model designer funded by the switzerland QGIS user group")
return help
def shortDescription(self):
return "This algorithm waits for a number of seconds effectively suspending the execution if used withing a model."
def groupId(self) -> str:
return ''
def createInstance(self):
return self.__class__()
This algorithm waits for a number of seconds effectively suspending the execution if used withing a model.
This is basically like the sleep function in python, but with a processing feedback around.
This is mainly useful in the context of the model designer to wait for some results, delays, simulate long operation, or for debugging purpose
Initially created to debug improvement to the model designer funded by the switzerland QGIS user group
Thank you!
Reviewed by gabrieldeluca 23 hours ago
This Processing Script is made available under the CC-0 license.